标签:kmp
Vasya has recently learned at school what a number’s divisor is and decided to determine a string’s divisor. Here is what he came up with.
String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string “abab” has two divisors — “ab” and “abab”.
Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.
Input
The first input line contains a non-empty string s1.
The second input line contains a non-empty string s2.
Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.
Output
Print the number of common divisors of strings s1 and s2.
Sample test(s)
Input
abcdabcd
abcdabcdabcdabcd
Output
2
Input
aaa
aa
Output
1
Note
In first sample the common divisors are strings “abcd” and “abcdabcd”.
In the second sample the common divisor is a single string “a”. String “aa” isn’t included in the answer as it isn’t a divisor of string “aaa”.
求2个串的”公约串”,分别kmp求出最小循环节
当然如果最小的循环节都不一样,答案显然就是0
设S1 = A^X
设s2 = B^Y
接下来只要求A和B的公约数的个数就行了
/*************************************************************************
> File Name: CF-117-D.cpp
> Author: ALex
> Mail: zchao1995@gmail.com
> Created Time: 2015年03月30日 星期一 15时42分33秒
************************************************************************/
#include <functional>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
using namespace std;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
const int N = 200100;
int nxt[N];
char str1[N], str2[N];
bool has[N];
char p1[N], p2[N];
void get_next(char str[], int nxt[])
{
nxt[0] = -1;
int j = 0;
int k = -1;
int len = strlen(str);
while (j < len)
{
if (k == -1 || str[j] == str[k])
{
nxt[++j] = ++k;
}
else
{
k = nxt[k];
}
}
}
int main()
{
while (~scanf("%s%s", str1, str2))
{
int len1 = strlen(str1);
get_next(str1, nxt);
int cnt1, cnt2;
if (len1 % (len1 - nxt[len1]))
{
cnt1 = len1;
strcpy(p1, str1);
}
else
{
cnt1 = len1 - nxt[len1];
for (int i = 0; i < cnt1; ++i)
{
p1[i] = str1[i];
}
p1[cnt1] = ‘\0‘;
}
get_next(str2, nxt);
int len2 = strlen(str2);
if (len2 % (len2 - nxt[len2]))
{
cnt2 = len2;
strcpy(p2, str2);
}
else
{
cnt2 = len2 - nxt[len2];
for (int i = 0; i < cnt2; ++i)
{
p2[i] = str2[i];
}
p2[cnt2] = ‘\0‘;
}
if (strcmp(p1, p2))
{
printf("0\n");
continue;
}
int x1 = len1 / cnt1;
int x2 = len2 / cnt2;
memset(has, 0, sizeof(has));
for (int i = 1; i * i <= x1; ++i)
{
if (x1 % i == 0)
{
has[i] = has[x1 / i] = 1;
}
}
int ans = 0;
for (int i = 1; i * i <= x2; ++i)
{
if (x2 % i == 0)
{
if (has[i])
{
++ans;
}
if (has[x2 / i] && i * i != x2)
{
++ans;
}
}
}
printf("%d\n", ans);
}
return 0;
}
Codeforces Round #117 (Div. 2)---D. Common Divisors
标签:kmp
原文地址:http://blog.csdn.net/guard_mine/article/details/44753313