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. 小米note3的开发者选项在哪里?怎么进入开发者模式?如何显示布局边界?

    小米note3的开发者选项在哪里?小米note3怎么进入开发者模式1.找到[设置],打开2.点击[我的设备]3.点击[全部参数]4.连续点击[MIUI版本]5次5.之后就会看见提示 “进入到开发者模式 ...

  3. 用Javascript,DHTML控制表格的某一列的显示与隐藏

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or ...

  4. 计蒜客---N的-2进制表示

    对于十进制整数N,试求其-2进制表示. 例如,因为  1*1  +  1*-2  +  1*4  +  0*-8  +1*16  +  1*-32  =  -13  ,所以(-13)_10  =  ( ...

  5. bzoj1180 tree

    题目链接 link cut tree 模板题 link cut tree不都是模板题嘛?(雾 #include<algorithm> #include<iostream> #i ...

  6. python URLError,HTTPError 的异常处理

    URLError,HTTPError 的异常处理 1. URLErrorURLError产生的原因1). 网络无连接2). 连接不到特定的服务器3). 服务器不存在 # 例子 import urlli ...

  7. Hadoop HA方案调研

    原文成文于去年(2012.7.30),已然过去了一年,很多信息也许已经过时,不保证正确,与Hadoop学习笔记系列一样仅为留做提醒. ----- 针对现有的所有Hadoop HA方案进行调研,以时间为 ...

  8. vue2+animate.css

    下载animate.css并引入项目, import './css/animate.css'使用: <template> <div class="box"> ...

  9. web项目错误—Java.util.ConcurrentMidificationException

    源代码: Iterator<String> iterator = list.iterator(); synchronized(synObject) { while(iterator.has ...

  10. 输出链表中倒数第k个节点

    题目描述 输入一个链表,输出该链表中倒数第k个结点. struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x ...