HDU 3746 Cyclic Nacklace(KMP最小覆盖子串)
2015-07-17 10:56
344 查看
KMP求出f数组后,最小覆盖子串为长度为len-f[len]的前缀。证明在前面的文章里。
代码:
代码:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; char c[100005]; int T; int f[100005]; int main(){ scanf("%d",&T); while(T--){ scanf("%s",c); f[0]=f[1]=0; int len=strlen(c); for(int i=1;i<len;i++){ int j=f[i]; while(j&&c[i]!=c[j]) j=f[j]; f[i+1]=(c[i]==c[j]?j+1:0); } int cir=len-f[len]; int res=0; if(cir==len) res=len; else { if(len%cir==0) res=0; else res=cir-len%cir; } printf("%d\n",res); } return 0; }
相关文章推荐
- websocke前世今生
- gtest简短,简单易用
- ******IT公司面试题汇总+优秀技术博客汇总
- 深入理解计算机系统读书笔记之第一章:漫游
- intel dpdk IPv4 Fragmentation Sample Application 测试
- Appfuse:添加自定义页面组件
- 格式化XML工具类
- 黑马程序员-----------集合框架
- Spring之构造注入与设值注入
- sublime Text2 2.0.2 build 2221 64位 破解(已测试)
- [leetcode-237]Delete Node in a Linked List(C语言)
- Tools:downloading and Building EDK II工具篇:安装/使用EDKII源码获取/编译工具[2.3]
- scp ssh自动填入密码
- Struct对齐方式
- 泛函编程(34)-泛函变量:处理状态转变-ST Monad
- ECMAScript 6教程 (二) 对象和函数
- leetcode:Lowest Common Ancestor of a Binary Search Tree
- 查看LINUX版本
- Scala之哈希表使用探究
- Scala之数组