Codeforces Round #117 (Div. 2) D.Common Divisors(KMP最小循环节)
http://codeforces.com/problemset/problem/182/D
题意:
如果把字符串a重复m次可以得到字符串b,那么我们称字符串a为字符串b的一个因子,现在给定两个字符串S1和S2,求它们的公共因子个数。
思路:
先求最小循环节,如果最小循环节不同,那么肯定是没有公共因子的。如果相同的话,那就看循环节长度为1,2,3...是否可行。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1e5+; char s1[maxn],s2[maxn];
int f1[maxn],f2[maxn]; int getfail(char* s,int* f)
{
int m = strlen(s);
f[] = ,f[] = ;
for(int i=;i<m;i++)
{
int j = f[i];
while(j && s[i]!= s[j]) j = f[j];
f[i+] = s[i] == s[j]?j+:;
}
int loop = m - f[m];
if(m%loop == ) return loop;
else return m;
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%s%s",s1,s2))
{
int n1 = strlen(s1);
int n2 = strlen(s2);
int k1 = getfail(s1,f1);
int k2 = getfail(s2,f2);
if(k1!=k2) {puts("");return ;}
for(int i=;i<k1;i++)
{
if(s1[i]!=s2[i]) {puts("");return ;}
}
int ans = ;
int t1 = n1/k1, t2 = n2/k2;
for(int i=;i<=t1 && i<=t2;i++)
if(t1%i== && t2%i==) ans++;
printf("%d\n",ans);
}
return ;
}
Codeforces Round #117 (Div. 2) D.Common Divisors(KMP最小循环节)的更多相关文章
- Codeforces Round #117 (Div. 2)
Codeforces Round #117 (Div. 2) 代码 Codeforces Round #117 (Div. 2) A. Battlefield any trench in meters ...
- Codeforces Round #608 (Div. 2) E. Common Number
链接: https://codeforces.com/contest/1271/problem/E 题意: At first, let's define function f(x) as follow ...
- Codeforces Round #659 (Div. 2) A. Common Prefixes
题目链接:https://codeforces.com/contest/1384/problem/A 题意 构造 $n+1$ 个字符串,使得 $n$ 对相邻字符串的相同前缀长度对应于数组 $a$ . ...
- Codeforces Round #608 (Div. 2) E - Common Number (二分 思维 树结构)
- Codeforces Round #608 (Div. 2) E. Common Number (二分,构造)
题意:对于一个数\(x\),有函数\(f(x)\),如果它是偶数,则\(x/=2\),否则\(x-=1\),不断重复这个过程,直到\(x-1\),我们记\(x\)到\(1\)的这个过程为\(path( ...
- Codeforces Round #376 (Div. 2) A. Night at the Museum —— 循环轴
题目链接: http://codeforces.com/contest/731/problem/A A. Night at the Museum time limit per test 1 secon ...
- Codeforces Round #282 (Div. 1)B. Obsessive String KMP+DP
B. Obsessive String Hamed has recently found a string t and suddenly became quite fond of it. He s ...
- Codeforces Round #344 (Div. 2) D. Messenger (KMP)
D. Messenger time limit per test2 seconds memory limit per test512 megabytes inputstandard input out ...
- Educational Codeforces Round 117 (Rated for Div. 2)
Educational Codeforces Round 117 (Rated for Div. 2) A. Distance https://codeforces.com/contest/1612/ ...
随机推荐
- 设计模式之Strategy(策略)(转)
Strategy是属于设计模式中 对象行为型模式,主要是定义一系列的算法,把这些算法一个个封装成单独的类. Stratrgy应用比较广泛,比如, 公司经营业务变化图, 可能有两种实现方式,一个是线条曲 ...
- intelj idea安装和配置
1|0优势 intellij idea 是目前公认的java最好的开发工具之一,商业版的IntelliJ应该包含了对 HTML5.CSS3.SASS.LESS.JavaScript.CoffeeScr ...
- 原生JavaScript插件编写指南(转载)
原生js开发指南 https://www.jianshu.com/p/e65c246beac1 在jQuery大量使用的环境下,目前网上的众多jQuery插件也能基本满足要求,但是在项目具体需求下,有 ...
- Jmeter压力测试和接口测试
jmeter是apache公司基于java开发的一款开源压力测试工具,体积小,功能全,使用方便,是一个比较轻量级的测试工具,使用起来非常简单.因为jmeter是java开发的,所以运行的时候必须先要安 ...
- Kali Linux没有无线网卡?玩个锤纸~
一.USB无限网卡 使用Kali linux,先准备好一个适合Kali系统的USB外置无限网卡,注意内置网卡并不适合渗透测试. Linux系统的指令相对于一般人来说比较晦涩难懂,最好选择免驱动类型,省 ...
- Camera2点击对焦实现
https://www.jianshu.com/p/76225ac72b56 android从5.0开始,废弃了原有的Camera接口,提供了全新的Camera2接口.Camera2接口为了给app提 ...
- git小技巧--如何从其他分支merge个别文件或文件夹
在实际工作中,一个大型的项目或版本迭代可能不是一次上线,可能会分好几次上线,这时候就会涉及创建多个分支,进行分别开发. 创建分支 功能分为2个分支,分别为A.B. A上面有个列表页功能 B上面有个详情 ...
- Golang操作结构体、Map转化为JSON
结构体生成Json package main import ( "encoding/json" "fmt" ) type IT struct { Company ...
- tomcat部署solr4.10
1.创建solrHome mkdir /opt/solrHome 2.拷贝solr基础数据 /example/solr/* /opt/solrHome 将contrib和dist两个目录拷贝到/opt ...
- The POM for XXX is invalid, transitive dependencies (if any) will not be available解决方案
今天,某个开发的环境在编译的时候提示警告The POM for XXX is invalid, transitive dependencies (if any) will not be availab ...