Codeforces Round #117 (Div. 2) D. Common Divisors

原题地址:http://codeforces.com/contest/182/problem/D

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

思路:可以预处理出两两个字符串长度的公共约数,因此用哈徐去判断某一长度的约数是否可行就行了.

#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <cctype>
#define eps 1e-8
#define INF 0x3f3f3f3f
#define MOD 1e9+7
#define PI acos(-1)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define CLR(x,y) memset((x),y,sizeof(x))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int seed = 131;
const int maxn = 1e5 + 5;
char a[maxn];
char s[maxn];
unordered_map<ull, int>mp;
vector<int>v;
ull h[3][maxn], p[maxn];
ull get_has(int flag, int l, int r) {
    return h[flag][r] - h[flag][l - 1] * p[r - l + 1];
}
int main() {
    p[0] = 1;
    for(int i = 1; i <= 1e5; i++) p[i] = p[i - 1] * seed;
    scanf("%s%s", a + 1, s + 1);
    int lena = strlen(a + 1);
    int lens = strlen(s + 1);
    for(int i = 1; i <= sqrt(lena); i++) {
        if(lena % i == 0) {
            if(lens % i == 0)v.push_back(i);
            if(lens % (lena / i) == 0 && i != lena / i) v.push_back(lena / i);//注意同一个数不能重复计算
        }
    }
    for(int i = 1; i <= lena; i++) h[1][i] = h[1][i - 1] * seed + a[i];
    for(int i = 1; i <= lens; i++) h[2][i] = h[2][i - 1] * seed + s[i];
    int ans = 0;
    for(int i = 0; i < v.size(); i++) {
        int num = v[i];
        int flag = 1;
        ull STD = get_has(1, 1, num);
        int cas = lena / num;
        for(int j = 0; j < cas; j++) {//枚举判断a
            ull my = get_has(1, j * num + 1, (j + 1) * num);
            if(my != STD) flag = 0;
        }
        cas = lens / num;
        for(int j = 0; j < cas; j++) {//枚举判断s
            ull my = get_has(2, j * num + 1, (j + 1) * num);
            if(my != STD) flag = 0;
        }
        if(flag) ans++;//可行的话,ans++
    }
    printf("%d\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yiqzq/article/details/81536985