HDU3746 Cyclic Nacklace KMP

http://acm.hdu.edu.cn/showproblem.php?pid=3746

 依然是next数组的操作...找循环节并且补全
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 using namespace std;
 8 const int maxn=1000010;
 9 const double eps=1e-8;
10 const long long modn=1000;
11 char a[maxn]={};
12 int nex[maxn]={};
13 int n;
14 int doit(){
15     int i=0,j=-1;nex[0]=-1;
16     while(i<n){
17         if(j==-1||a[j]==a[i]){
18             nex[++i]=++j;
19         }else{
20             j=nex[j];
21         }
22     }
23     int z=n-nex[n];
24     if(nex[n]==0){
25         return n;
26     }else if(n%z==0){
27         return 0;
28     }
29     else {
30         return z-n%z;
31     }
32 }
33 int main(){
34     int T;
35     scanf("%d",&T);
36     while(T-->0){
37         scanf("%s",&a);
38         n=strlen(a);
39         printf("%d\n",doit());
40     }
41     return 0;
42 }
View Code

 

转载于:https://www.cnblogs.com/137shoebills/p/7786483.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值