博客
关于我
CSU 1353: Guessing the Number(字符串周期问题+字符串最小表示法)
阅读量:607 次
发布时间:2019-03-12

本文共 2205 字,大约阅读时间需要 7 分钟。

题目:

Description

    Alice thought out a positive integer x, and x does not have leading zeros. She writes x several times in a line one by one and gets a digits sequence.

    For example, suppose the number Alice thought out is 231 and she writes it 4 times, then she will get the digits sequence 231231231231.
    Now Alice just shows you a continuous part of that digits sequence, and could you find out the minimum probable value of x?
    For example, if Alice showed you 31231, x may be 312, because you will get 312312 if you writes it 2 times, and 31231 is a continuous part of 312312. Obviously x may also be 231, 1231, 31231, 231731, etc. But the minimum probable value of x is 123, write it 3 times and you will see 31231 is a continuous part of 123123123.

Input

    The first line has one integer T, means there are T test cases.

    For each test case, there is only one digits sequence in a line. The number of digits is in range [1, 105].
    The size of the input file will not exceed 5MB.

Output

    For each test case, print the minimum probable value of x in one line.

Sample Input

477731231401230

Sample Output

71231234010

思路:

首先根据kmp求出字符串的周期,参考

然后字符串最小表示法有个专门的方法,思想和kmp很像

代码:

#include
#include
using namespace std;char c[200005];int next_[100005];int l;void get_next(char *t, int m, int next[]){ int i = 1, j = 0; next[0] = next[1] = 0; while (i < m) { if (j == 0 || t[i] == t[j])next[++i] = ++j; else j = next[j]; }}int getmin(int n){ int i = 1, j = 2, k = 0; while (i <= n && j <= n && k <= n) { if (c[i] == '0')i++; else if (c[j] == '0')j++; else if (i == j)j++; else if (c[i + k] == c[j + k])k++; else if (c[i + k] > c[j + k])i += k + (!k), k = 0; else j += k + (!k), k = 0; } return (i <= n) ? i : j;}int main(){ int t; scanf("%d", &t); c[0] = '0'; while (t--) { scanf("%s", &c[1]); l = strlen(c + 1); bool flag = true;//全0 for (int i = 1; i <= l; i++)if (c[i] != '0')flag = false; if (flag) { printf("1%s\n", c + 1); continue; } get_next(c, l, next_); int k = next_[l]; while (k && c[l] != c[k])k = next_[k]; l -= k; for (int i = l + 1; i <= l * 2; i++)c[i] = c[i - l]; int key = getmin(l); c[key + l] = '\0'; printf("%s\n", c + key); } return 0;}

转载地址:http://wtlxz.baihongyu.com/

你可能感兴趣的文章
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>
multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
查看>>
mysql csv import meets charset
查看>>
multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
查看>>
MySQL DBA 数据库优化策略
查看>>
multi_index_container
查看>>
MySQL DBA 进阶知识详解
查看>>
Mura CMS processAsyncObject SQL注入漏洞复现(CVE-2024-32640)
查看>>