hdu 3746 kmp求循环节

题意就是将所给的字符串变成多个完整的循环(至少两个),然后给出最少需要添加的字符数。

http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<string.h>
 4 #include<algorithm>
 5 using namespace std;
 6 const int MAXN=100010;
 7 char str[MAXN];
 8 int next[MAXN];
 9
10 void getNext(char *p)
11 {
12     int j,k;
13     j=0;
14     k=-1;
15     int len=strlen(p);
16     next[0]=-1;
17     while(j<len)
18     {
19         if(k==-1||p[j]==p[k])
20         {
21             j++;
22             k++;
23             next[j]=k;
24         }
25         else k=next[k];
26     }
27 }
28 int main()
29 {
30     int T;
31     scanf("%d",&T);
32     while(T--)
33     {
34         scanf("%s",&str);
35         getNext(str);
36         int len=strlen(str);
37         if(next[len]==0)
38         {
39             printf("%d\n",len);
40             continue;
41         }
42         int t=len-next[len];
43         if(len%t==0)printf("0\n");
44         else
45         {
46             printf("%d\n",t-len%t);
47         }
48     }
49     return 0;
50 }
时间: 2024-10-14 01:13:22

hdu 3746 kmp求循环节的相关文章

HDU 3746 Cyclic Nacklace(KMP求循环节)

Description CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspir

HDU - 3746 Cyclic Nacklace (KMP求循环节)

Description CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspir

【HDU 3746】Simpsons’ Hidden Talents(KMP求循环节)

求next数组,(一般有两种,求循环节用的见代码)求出循环节的长度. #include <cstdio> #define N 100005 int n,next[N]; char s[N]; int main(){ scanf("%d",&n); while(n--){ scanf("%s",s); int i=0,k=-1; next[0]=k; while(s[i]){ if(k==-1||s[i]==s[k]) { i++; k++; ne

17999 Light-bot 模拟 + kmp求循环节

http://acm.scau.edu.cn:8000/uoj/mainMenu.html 17999 Light-bot 时间限制:1000MS  内存限制:65535K 提交次数:0 通过次数:0 题型: 编程题   语言: 不限定 Description I (you needn't know who am "I".) am currently playing a game called "Light-bot". In the game, the "

hdu3746 kmp求循环节

CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspired by the en

HDU3746 Cyclic Nacklace KMP求循环节

HDU3746给定一个字符串,求:在该字符串末尾最少补充多少个字符,可以使得这个字符串获得周期性. 周期性指存在一个子串,使得该字符串可以正好分解成若干个这个子串(数量要大于1). Input 第一行是一个整数 T ( 0<T<=100 ) 代表测试数据的组数. 之后T行每行一个字符串,由小写字母组成,字符串的长度3<=L<=100000. Output 每组数据输出一行结果. Sample Input 3 AAA ABCA ABCDE Sample Output 0 2 5 本题

Cyclic Nacklace HDU 3746 KMP 循环节

Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len]-len的差即是循环部分的长度. 这个是重点.这个题目自己开始没有想明白,看的博客,推荐这个. 代码实现 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const i

hdu 4291 矩阵幂 循环节

http://acm.hdu.edu.cn/showproblem.php?pid=4291 凡是取模的都有循环节-----常数有,矩阵也有,而且矩阵的更神奇: g(g(g(n))) mod 109 + 7  最外层MOD=1e9+7  可以算出g(g(n))的循环节222222224,进而算出g(n)的循环节183120LL,然后由内而外计算即可 注释掉的是求循环节的代码 //#pragma comment(linker, "/STACK:102400000,102400000")

区间DP+next求循环节 uva 6876

1 // 区间DP+next求循环节 uva 6876 2 // 题意:化简字符串 并表示出来 3 // 思路:dp[i][j]表示 i到j的最小长度 4 // 分成两部分 再求一个循环节 5 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <cstdio> 10 #include <vector> 11 #include <cmath