HUST 1010 The Minimum Length (kmp求最小循环节)

来源:互联网 发布:柚子网络用语意思 编辑:程序博客网 时间:2024/06/04 19:30

题目链接:点击打开链接


1010 - The Minimum Length

时间限制:1秒 内存限制:128兆

题目描述
There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I got a new string: AAAAAA...... Now I cut it from two different position and get a new string B. Then, give you the string B, can you tell me the length of the shortest possible string A.For example, A="abcdefg". I got abcdefgabcdefgabcdefgabcdefg.... Then I cut the red part: efgabcdefgabcde as string B. From B, you should find out the shortest A.
输入
Multiply Test Cases.For each line there is a string B which contains only lowercase and uppercase charactors.The length of B is no more than 1,000,000.
输出
For each line, output an integer, as described above.
样例输入
bcabcabefgabcdefgabcde
样例输出
37
题目大意:给你一串字符串,问这个字符串是由长度多少的字符串重复组成,也就是循环节。用kmp求循环节就好了

#include <stdio.h>#include <string.h>#include <iostream>using namespace std;char st[1000100];int next[1001000];void getnext(char st[],int m){    int j = 0 ,k = -1;    next[0] = -1;    while (j < m)    {        if (k == -1 || st[j] == st[k] )        {            ++j;            ++k;            next[j] = k;        }        else         {            k = next[k];        }        //printf("%d\n",next[j]);    }}int main(){    while (cin >> st)    {        int len = strlen(st);        getnext(st,len);        if(len == 1)             printf("1\n");          printf("%d\n",len - next[len]);    }    return 0;}




0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 斗战神师徒一个人删除角色怎么办 起业kx5防盗器不响怎么办 灌浆记录仪存盘满了怎么办 自首后发现无罪证据怎么办 中国劲酒过期了怎么办 玻尿酸流到眼皮怎么办 手指被胶带缠紫了怎么办 打玻尿酸不平整怎么办 孩子被老师体罚我该怎么办 孩子妈妈入狱了我该怎么办 机顶盒电视收不到台怎么办 跳芭蕾舞下面硬起来了怎么办 深情密码结局赵深深怎么办 宝宝头着地摔了怎么办 小孩头着地摔了怎么办 头朝下墩了脖子怎么办 两岁摔倒头着地怎么办 马桶大便冲不下去怎么办 脚丫吧里起泡痒怎么办 脚受伤后肿了怎么办 摔到了腿受伤了怎么办 骑车摔倒了肿了怎么办 生活大冒险老婆失踪了怎么办 手机qq图裂了怎么办 qq发的图裂了怎么办 抖音账号搬运多怎么办 别人搜不到我QQ怎么办 qq号被限制解封怎么办 买的桌子会晃怎么办 车子座椅皮坏了怎么办 裤子被椅子刮了怎么办 脚麻了被别人动怎么办 毛孔又粗又黑怎么办 手臂睡觉压麻了怎么办 睡觉压的胳膊麻怎么办 摔跤摔到腰肿了怎么办 牛奶喝多了拉稀怎么办 doc文档下载是乱码怎么办 家人被教练技术骗了怎么办 十个月宝宝不爱喝水怎么办 离婚后孩子找爸爸怎么办