D. Common Divisors CF 182D

来源:互联网 发布:怎么打罗马数字mac 编辑:程序博客网 时间:2024/04/28 02:38
#include<stdio.h>#include<string.h>typedef __int64 lld;void build_next(int len_t,char T[],int next[]){int k, q, p, a;next[0] = len_t;for (k = 1, q = -1; k < len_t; k ++, q--) {if (q < 0 || k + next[k - a] >= p) {if (q < 0)q = 0, p = k;while (p < len_t && T[p] == T[q]) {p ++, q ++;}next[k] = q, a = k;}else {next[k] = next[k - a];}}}int len_s,len_t;char T[100005];char S[100005];int next1[100005];int next2[100005];int main(){int i,j;while(scanf("%s%s",T,S)!=EOF){len_s=strlen(S);len_t=strlen(T);build_next(len_s,S,next1);build_next(len_t,T,next2);int ans=0;next1[len_s]=next2[len_t]=0;for(i=1;S[i-1]==T[i-1] && i<=len_s && i<=len_t;i++)//从长度为1...len枚举重复长度{if(len_s%i==0 && len_t%i==0 && next1[i]==len_s-i && next2[i]==len_t-i)ans++;}printf("%d\n",ans);}return 0;}

原创粉丝点击