会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
c语言源码
Codeforces 182D - Common Divisors
KMP求最小循环节
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> char s1[100005],s2[100005]; int next[100005],flag; void getnext(char * s){ int k=1,j=0,len=strlen(&s[1]); next[k]= 0; while(k<=len ){ if( j== 0|| s[k]== s[j] ){ ++j, ++k; next[k]= j; } else j= next[j]; } } int min(int a,int b){ return a>b?b:a; } int main() { int a,b,i,j; scanf("%s",&s1[1]); getnext(s1); int len1= strlen(&s1[1]);//注意s[len+1]='\0' a= len1- next[len1+1]+1; if(len1%a==0) len1/=a; else a=len1; scanf("%s",&s2[1]); memset(next,0,sizeof(next)); getnext(s2); int len2= strlen(&s2[1]);//注意s[len+1]='\0' b= len2- next[len2+1]+1; if(len2%b==0) len2/=b; else b=len2; if(a!=b){ printf("0\n"); } else{ for(i=1;i<=a;i++){ if(s1[i]!=s2[i]){ printf("0\n"); break; } } if(i==a+1){ int tem=min(len1,len2),sum=0; for(j=1;j<=tem;j++){ if(len1%j==0 && len2%j==0) sum++; } printf("%d\n",sum); } } }
posted on
2012-04-25 09:22
c语言源码
阅读(
182
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
导航
博客园
首页
新随笔
联系
订阅
管理
公告