hdu 1358 Period(给定一个字符串,求有多少个前缀(包括自己本身),它是由k(k>2,并且尽量大)个循环节组成的)
2015-08-04 11:03
375 查看
代码:
#include<cstdio> #include<cstring> using namespace std; int LCPS[1000005]; int next[1000005]; char s[1000005]; int n; void GetLCPS() { int j=0; int k=-1; next[0]=-1; while(j<n) { if(k==-1||s[k]==s[j]) { LCPS[j++]=++k; next[j]=k; } else { if(k-1>=0) k=LCPS[k-1]; else k=-1; } } } int main() { int cnt=0; while(scanf("%d",&n)&&n) { printf("Test case #%d\n",++cnt); scanf("%s",s); GetLCPS(); for(int i=1;i<n;i++) { int cir=(i+1)-(next[i+1]); int r=(i+1)%cir; int k=(i+1)/cir; if(r==0&&k>1) { printf("%d %d\n",i+1,k); } } printf("\n"); } return 0; }
相关文章推荐
- 1072:字母排列
- 语义化版本2.0.0
- 转:C++编程隐蔽错误:error C2533: 构造函数不能有返回类型
- sql 系统表协助集合
- shell的含义
- 从零开始学Python
- 如何在DAVE4 SDK import已有的APP
- Same Tree
- linux 下编译安装Qt
- 微信服务号 网站服务器不能直接获取用户session的问题
- Linux cronolog
- 实用小技巧(一)
- try
- Struts2组合spring
- 0-1排序引理
- 南邮 OJ 1095 奇特的图形
- 判断js是否被禁用
- 常用做action recognition 的数据集介绍
- C++中friend的使用(friend function and friend class)
- 软件测试博客较好的网址(供参考)