182 D. Common Divisors

题目链接

这个题,说不清出算那种题。题目还是挺不错的。

题意:两个字符串的公共因子,因子的定义是如abab因子有 ab,abab两个。

开始的时候很明显就可以发现,要对长度分解因子,然后判断是否分别是字符串的因子,然后这两个因子是否相同。

 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cstdlib>

 5 #include <map>

 6 #include <algorithm>

 7 using namespace std;

 8 int o[200001],len1,len2;

 9 char s1[200001],s2[200001];

10 int judge(int x)

11 {

12     int i;

13     for(i = 0; i <= x-1; i ++)

14     {

15         if(s1[i] != s2[i])

16             return 0;

17     }

18     for(i = x; i <= len1-1; i ++)

19     {

20         if(s1[i] != s1[i%x])

21             return 0;

22     }

23     for(i = x; i <= len2-1; i ++)

24     {

25         if(s2[i] != s2[i%x])

26             return 0;

27     }

28     return 1;

29 }

30 int main()

31 {

32     int i,num;

33     scanf("%s%s",s1,s2);

34     len1 = strlen(s1);

35     len2 = strlen(s2);

36     for(i = 1; i*i <= len1; i ++)

37     {

38         if(len1%i == 0)

39         {

40             o[i] ++;

41             if(i*i != len1)

42             {

43                 o[len1/i] ++;

44             }

45         }

46     }

47     for(i = 1; i*i <= len2; i ++)

48     {

49         if(len2%i == 0)

50         {

51             o[i] ++;

52             if(i*i != len2)

53             {

54                 o[len2/i] ++;

55             }

56         }

57     }

58     num = 0;

59     for(i = 1; i <= len1||i <= len2; i ++)

60     {

61         if(o[i] == 2)

62         {

63             if(judge(i))

64                 num ++;

65         }

66     }

67     printf("%d\n",num);

68     return 0;

69 }

你可能感兴趣的:(com)