Codeforces D. Common Divisors KMP

版权声明:转载请标明出处 https://blog.csdn.net/weixin_41190227/article/details/86509492

D. Common Divisors

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples

input

Copy

abcdabcd
abcdabcdabcdabcd

output

Copy

2

input

Copy

aaa
aa

output

Copy

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".

题目大意:如果把一个字符串s1重复n次得到s2, 就把s1叫做s2的一个“公约串”, 给出两个字符串, 求有多少个“公约串”。

解题思路: 先求出两个串的最小循环节,求循环节用到KMP的Next数组。判断这两个串的最小循环节是否一样,相同才能有解,否则无解。在有解的情况下每次都增加最小循环节的长度,判断下是否可以整除即可。

/*
@Author: Top_Spirit
@Language: C++
*/
#include <bits/stdc++.h>
using namespace std ;
typedef unsigned long long ull ;
typedef long long ll ;
const int Maxn = 1e5 + 10  ;
const int INF = 0x3f3f3f3f ;
const double PI = acos(-1.0) ;
const ull seed = 133;
const ull MOD = 1e9 + 7 ;

string s1, s2 ;
int len1, len2 ;
int Next[Maxn] ;

int Get_Next(string s){
    int len = s.size() ;
    Next[0] = -1 ;
    int j = 0, k = -1 ;
    while (j < len){
        if (k == -1 || s[j] == s[k]) Next[++j] = ++k ;
        else k = Next[k] ;
    }
    return Next[len] ;
}

int main (){
    cin >> s1 >> s2 ;
    len1 = s1.size() ;
    len2 = s2.size() ;
    int m, n ;
    n = Get_Next(s1) ;
    memset(Next, 0, sizeof(Next)) ;
    m = Get_Next(s2) ;
    int a = 0, b = 0 ;
    if (len1 % (len1 - n) == 0) a = len1 - n ;
    else a = len1 ;
    if (len2 % (len2 - m) == 0) b = len2 - m ;
    else b = len2 ;
//    cout << a << " " << b << endl ;
    if (a == b){
        for (int i = 0; i < a; i++){
            if (s1[len1 - i - 1] != s2[len2 - i - 1]){
                cout << 0 << endl ;
                return 0 ;
            }
        }
        int tmp = a , ans = 0;
        int len = min(len1, len2) ;
        while (tmp <= len){
            if (len1 % tmp == 0 && len2 % tmp == 0) ans++ ;
            tmp += a ;
        }
        cout << ans << endl ;
    }
    else cout << 0 << endl ;
    return 0 ;
}

如果我爱我自己

就会日出而作,日落而息

猜你喜欢

转载自blog.csdn.net/weixin_41190227/article/details/86509492
今日推荐