http://codeforces.com/problemset/problem/182/D

题意:
如果把字符串a重复m次可以得到字符串b,那么我们称字符串a为字符串b的一个因子,现在给定两个字符串S1和S2,求它们的公共因子个数。

思路:

先求最小循环节,如果最小循环节不同,那么肯定是没有公共因子的。如果相同的话,那就看循环节长度为1,2,3...是否可行。

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1e5+; char s1[maxn],s2[maxn];
int f1[maxn],f2[maxn]; int getfail(char* s,int* f)
{
int m = strlen(s);
f[] = ,f[] = ;
for(int i=;i<m;i++)
{
int j = f[i];
while(j && s[i]!= s[j]) j = f[j];
f[i+] = s[i] == s[j]?j+:;
}
int loop = m - f[m];
if(m%loop == ) return loop;
else return m;
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%s%s",s1,s2))
{
int n1 = strlen(s1);
int n2 = strlen(s2);
int k1 = getfail(s1,f1);
int k2 = getfail(s2,f2);
if(k1!=k2) {puts("");return ;}
for(int i=;i<k1;i++)
{
if(s1[i]!=s2[i]) {puts("");return ;}
}
int ans = ;
int t1 = n1/k1, t2 = n2/k2;
for(int i=;i<=t1 && i<=t2;i++)
if(t1%i== && t2%i==) ans++;
printf("%d\n",ans);
}
return ;
}

Codeforces Round #117 (Div. 2) D.Common Divisors(KMP最小循环节)的更多相关文章

  1. Codeforces Round #117 (Div. 2)

    Codeforces Round #117 (Div. 2) 代码 Codeforces Round #117 (Div. 2) A. Battlefield any trench in meters ...

  2. Codeforces Round #608 (Div. 2) E. Common Number

    链接: https://codeforces.com/contest/1271/problem/E 题意: At first, let's define function f(x) as follow ...

  3. Codeforces Round #659 (Div. 2) A. Common Prefixes

    题目链接:https://codeforces.com/contest/1384/problem/A 题意 构造 $n+1$ 个字符串,使得 $n$ 对相邻字符串的相同前缀长度对应于数组 $a$ . ...

  4. Codeforces Round #608 (Div. 2) E - Common Number (二分 思维 树结构)

  5. Codeforces Round #608 (Div. 2) E. Common Number (二分,构造)

    题意:对于一个数\(x\),有函数\(f(x)\),如果它是偶数,则\(x/=2\),否则\(x-=1\),不断重复这个过程,直到\(x-1\),我们记\(x\)到\(1\)的这个过程为\(path( ...

  6. Codeforces Round #376 (Div. 2) A. Night at the Museum —— 循环轴

    题目链接: http://codeforces.com/contest/731/problem/A A. Night at the Museum time limit per test 1 secon ...

  7. Codeforces Round #282 (Div. 1)B. Obsessive String KMP+DP

    B. Obsessive String   Hamed has recently found a string t and suddenly became quite fond of it. He s ...

  8. Codeforces Round #344 (Div. 2) D. Messenger (KMP)

    D. Messenger time limit per test2 seconds memory limit per test512 megabytes inputstandard input out ...

  9. Educational Codeforces Round 117 (Rated for Div. 2)

    Educational Codeforces Round 117 (Rated for Div. 2) A. Distance https://codeforces.com/contest/1612/ ...

随机推荐

  1. c# 定义委托和使用委托(事件的使用)

    使用委托时要先实例化,和类一样,使用new关键字产生委托的新实例,然后将一个或者多个与委托签名匹配的方法与委托实例关联.随后调用委托时,就会调用所有与委托实例关联的方法. 与委托关联可以是任何类或者结 ...

  2. 产品经理-需求分析-用户故事-敏捷开发 详解 一张图帮你了解Scrum敏捷流程

    产品经理-需求分析-用户故事-敏捷开发 详解 用户故事是从用户的角度来描述用户渴望得到的功能.一个好的用户故事包括三个要素:1. 角色:谁要使用这个功能.2. 活动:需要完成什么样的功能.3. 商业价 ...

  3. vue之component

    因为组件是可复用的 Vue 实例,所以它们与 new Vue 接收相同的选项,例如 data.computed.watch.methods 以及生命周期钩子等.仅有的例外是像 el 这样根实例特有的选 ...

  4. 监控MySQL主从脚本

    示例一: cat check_mysql_health #!/bin/sh slave_is=($(mysql -S /tmp/mysql3307.sock -uroot -e "show ...

  5. 推荐:全新Java开发思维导图

    蓦然回首自己做开发已经十年了,这十年中我获得了很多,技术能力.培训.出国.大公司的经历,还有很多很好的朋友. 但再仔细一想,这十年中我至少浪费了五年时间,这五年可以足够让自己成长为一个优秀的程序员,可 ...

  6. log buffer space等待事件

    最近,我们有台服务器在delete操作期间发现一直在等待log buffer space,其他节点就没与这个问题.经查,向重做缓冲区上写入重做记录的进程,为了确保拥有重做缓冲区内必要的空间,需要获得r ...

  7. centos6下jbd2进程占用大量IO处理

    刚在尝试重现一个bug时,好像在killed mysql一段时间之后,io一直很高,如下: 12:40:01 PM CPU %user %nice %system %iowait %steal %id ...

  8. 关于sql中in 和 exists 的效率问题

    在用in的地方可以使用freemark标签代替,例如: 将 <#if assistantList??&& (assistantList?size > 0)> AND ...

  9. 20145208 蔡野 《网络对抗》Exp8 Web基础

    20145208 蔡野 <网络对抗>Exp8 Web基础 本实践的具体要求有: (1).Web前端HTML(1分) 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POS ...

  10. centos7 update network time

    yum install -y ntp crontab -e */5 * * * * /usr/bin/ntpdate ntp.api.bz ###   ntp.api.bz 是一组NTP集群服务器,之 ...