Algorithm


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



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <iostream>
#include <string>

using namespace std;

int const N = 1e5 + 1;

void kmpWork(string &s, int f[]) {
  for (int i = 1, j = 0; i < s.length(); ++i) {
		while (j != 0 && s[i] != s[j]) j = f[j - 1];
		if (s[i] == s[j]) ++j;
		f[i] = j;
	}
}

int kmpSearch(string &t, string &p, int f[]) {
  int res = 0;
  for (int i = 0, j = 0; i < t.length(); ++i) {
		while (j != 0 && t[i] != p[j]) j = f[j - 1];
		if (t[i] == p[j]) ++j;
		if(j == p.length()) {
		  res++;
		  j = 0;
		}
	}
	return res;
}

int main() {
  int af[N], bf[N];
  
  string a, b;
  cin >> a >> b;
  
  kmpWork(a, af);
  kmpWork(b, bf);
  
  int resa = a.length();
  if(a.length() % (a.length() - af[a.length() - 1]) == 0)
    resa = a.length() - af[a.length() - 1];
  string suba = a.substr(0, resa);
  
  int resb = b.length();
  if(b.length() % (b.length() - bf[b.length() - 1]) == 0)
    resb = b.length() - bf[b.length() - 1];
  string subb = b.substr(0, resb);
  
  if(suba != subb)
    cout << 0 << endl;
  else {
    int res = 0, me = 1, i, j;
    bool ok;
    
    while(me * resa <= a.length() && me * resb <= b.length()) {
      if(a.length() % (me * resa) != 0 || b.length() % (me * resb) != 0) {
        me++;
        continue;
      }
      
      ok = true;
      
      for(i = 0, j = 0; i < a.length(); ++i, ++j) {
        if(a[i] != suba[j]) {
          ok = false;
          break;
        }
        
        if(j == resa - 1)
          j = -1;
      }
      
      if(!ok) {
        ++me;
        continue;
      }
      
      for(i = 0, j = 0; i < b.length(); ++i, ++j) {
        if(b[i] != subb[j]) {
          ok = false;
          break;
        }
        
        if(j == resb - 1)
          j = -1;
      }
      
      res += ok;
      me++;
    }
    
    cout << res << endl;
  }
  
  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
abcdabcd
abcdabcdabcdabcd

Output

x
+
cmd
2
Advertisements

Demonstration


Codeforcess Solution Common Divisors, D. Common Divisors ,C,C++, Java, Js and Python , Common Divisors,Codeforcess Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+