HUST 1010(KMP应用:最短循环节点)
2016-02-17 17:16
549 查看
F - The Minimum Length
Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld
& %llu
Submit Status
Description
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 abcd efgabcdefgabcdefgabcdefg.... Then I cut the red part: efgabcdefgabcde as string B. From B, you should find out the shortest A.
Input
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.
Output
For each line, output an integer, as described above.
Sample Input
Sample Output
题意:有一个字符串S,它是由重复的一个子串组成的,例如:ABABABAB,那么这个子串就是:AB,现在给出串的一部分给你,例如:BAB。让你求这个子串
题解:错了很多次,诶,看了题解是:根据KMP的原理,是将前缀与后缀的最大匹配那么最后的fail
的值就是除了第一个子串剩下的长度,那么直接使用n-fail
就可以了,自己画了一些字符串,匹配了一下,这个结论确实是对的,还是需要理解理解吗,算是KMP的一种题型吧:最短循环节点
我们其实可以看一个例子:比如AAAAAAAAA
他的fail指针就是----------->>>>>0123456789
字符串每一次最大的匹配前缀,与后缀的长度,那么显然每匹配一次就在原来的基础长度上+1。
Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld
& %llu
Submit Status
Description
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 abcd efgabcdefgabcdefgabcdefg.... Then I cut the red part: efgabcdefgabcde as string B. From B, you should find out the shortest A.
Input
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.
Output
For each line, output an integer, as described above.
Sample Input
bcabcab efgabcdefgabcde
Sample Output
3 7
题意:有一个字符串S,它是由重复的一个子串组成的,例如:ABABABAB,那么这个子串就是:AB,现在给出串的一部分给你,例如:BAB。让你求这个子串
题解:错了很多次,诶,看了题解是:根据KMP的原理,是将前缀与后缀的最大匹配那么最后的fail
的值就是除了第一个子串剩下的长度,那么直接使用n-fail
就可以了,自己画了一些字符串,匹配了一下,这个结论确实是对的,还是需要理解理解吗,算是KMP的一种题型吧:最短循环节点
我们其实可以看一个例子:比如AAAAAAAAA
他的fail指针就是----------->>>>>0123456789
字符串每一次最大的匹配前缀,与后缀的长度,那么显然每匹配一次就在原来的基础长度上+1。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; #define N 1000000+5 char s ; int fail ; int ans; void getfail(char *p,int *f) { int m=strlen(p); f[0]=0;f[1]=0; for(int i=1;i<m;i++) { int j=f[i]; while(j&&p[i]!=p[j])j=f[j]; f[i+1]=p[i]==p[j]?j+1:0; } } int main() { #ifdef CDZSC freopen("i.txt","r",stdin); #endif while(~scanf("%s",s)) { ans=1; getfail(s,fail); int len=strlen(s); printf("%d\n",len-fail[len]); } return 0; }
相关文章推荐
- hdu 1421 搬寝室,dp
- 最新获取屏幕宽高
- LeetCode 165. Compare Version Numbers 解题报告
- 【转】Nginx搭建反向代理服务器过程详解
- PHP获取时间排除周六、周日的两个方法
- Java程序员常用工具类库 - 目录(转)
- 许朝军
- Relative 定位与Absolute 定位实例
- Lastpass——密码管理工具
- SourceTree 安装教程
- android window 一些属性说明
- HDU ACM 1050 Moving Tables(贪心 + 区间覆盖问题)
- Apache与Tomcat有什么关系和区别
- Dex文件结构及对应的数据结构
- Xcode LaunchImage 载入界面大小设置
- linux date -d 的一些使用方法
- 年后小结
- [转] 小tip: 使用CSS将图片转换成模糊(毛玻璃)效果 ---张鑫旭
- SQL 常用方法
- java基础