182 D. Common Divisors

本文介绍了一种寻找两个字符串的公共因子的算法实现。通过分解字符串长度并验证因子的重复性和一致性来解决问题。代码使用C++编写,展示了如何判断一个子串是否为两个输入字符串的有效公共因子。

题目链接

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

题意:两个字符串的公共因子,因子的定义是如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 }

转载于:https://www.cnblogs.com/naix-x/archive/2012/10/25/2738519.html

题目ID ID类型 原始答案(raw_input) 原始答案(review.gold) 原始答案一致性 模型答案(review.pred) 模型答案(boxed) 模型答案一致性 审核结果(review.result) 完成原因 原因分析 模型输出摘要 cmpl-e759f3d8-78e1-11f0-b2b4-1a85109d8f07 自定义ID 8 N/A N/A N/A 8 N/A N/A stop 正常完成 <think> Okay, so I need to figure out how many whole-number divisors there are for the integer given... cmpl-e759ed84-78e1-11f0-b2b4-1a85109d8f07 自定义ID 7 N/A N/A N/A 7 N/A N/A stop 正常完成 <think> Okay, so I need to figure out the value of $ n $ in this magic square. Let me start by recal... cmpl-e759f856-78e1-11f0-b2b4-1a85109d8f07 自定义ID -\sqrt{3} N/A N/A N/A -\sqrt{3} N/A N/A stop 正常完成 <think> Okay, so I need to simplify tan 100° + 4 sin 100°. Hmm, let me think about how to approach t... cmpl-e75996ae-78e1-11f0-b2b4-1a85109d8f07 自定义ID 17 N/A N/A N/A 17 N/A N/A stop 正常完成 <think> Okay, so I need to find the smallest distance between the origin and a point on the graph of... cmpl-e759f162-78e1-11f0-b2b4-1a85109d8f07 自定义ID 4 N/A N/A N/A 4 N/A N/A stop 正常完成 <think> Okay, so I need to figure out how many numbers are common factors of both 14 and 42. Let me ... cmpl-e759f630-78e1-11f0-b2b4-1a85109d8f07 自定义ID 1 N/A N/A N/A 1 N/A N/A stop 正常完成 <think> Okay, so I need to find the remainder when the sum of numbers from 1 to 10 is divided by 9. ... cmpl-40f2a44e-78e2-11f0-b2b4-1a85109d8f07 自定义ID \frac{1}{16} N/A N/A N/A \dfrac{1}{16} N/A N/A stop 正常完成 <think> Okay, so I need to simplify the product of these four cosine terms: cos(2π/15) * cos(4π/15) ... cmpl-772f0d90-78e2-11f0-b2b4-1a85109d8f07 自定义ID 14 N/A N/A N/A 14 N/A N/A stop 正常完成 <think> Okay, so I need to find the sum of the digits in the terminating decimal representation of t... 生成的csv文件内容,修改并重新优化脚本
08-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值