博客
关于我
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/

你可能感兴趣的文章
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>
mysql 添加索引
查看>>
MySQL 添加索引,删除索引及其用法
查看>>
mysql 状态检查,备份,修复
查看>>
MySQL 用 limit 为什么会影响性能?
查看>>
MySQL 用 limit 为什么会影响性能?有什么优化方案?
查看>>
MySQL 用户权限管理:授权、撤销、密码更新和用户删除(图文解析)
查看>>
mysql 用户管理和权限设置
查看>>
MySQL 的 varchar 水真的太深了!
查看>>
mysql 的GROUP_CONCAT函数的使用(group_by 如何显示分组之前的数据)
查看>>