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/ ...
随机推荐
- TCP客户端图片上传服务端保存本地示例
//TCP客户端public class TCPClient { public static void main(String[] args)throws IOException { Socket s ...
- kali 开启ssh服务
1. 一.配置SSH参数 修改sshd_config文件,命令为: vi /etc/ssh/sshd_config 将#PasswordAuthentication no的注释去掉,并且将NO修 ...
- 15类Android通用流行框架
15类Android通用流行框架 Android流行框架 缓存 DiskLruCache Java实现基于LRU的磁盘缓存 图片加载 Android Universal Image Loader 一个 ...
- JavaScript使用localStorage缓存Js和css文件
对于WebApp来说,将js css文件缓存到localstorage区可以减少页面在加载时与HTTP请求的交互次数,从而优化页面的加载时间.特别是当移端信号不好高延迟时优化效果还是很显见的 下面的代 ...
- 进程表示之进程ID号
UNIX进程总是会分配一个号码用于在其命名空间总唯一地标识它们,该号码称作进程ID号,简称PID. 1.进程ID 但每个进程除了PID外,还有其他的ID,有下列几种可能的类型: (1)处于某个线程组中 ...
- python-数据分析与展示(Numpy、matplotlib、pandas)---2
笔记内容整理自mooc上北京理工大学嵩天老师python系列课程数据分析与展示,本人小白一枚,如有不对,多加指正 1.python自带的图像库PIL 1.1常用API Image.open() ...
- Centos7 安装 apache + php7.0 环境
安装apache rpm -qa|grep httpd 查看是否安装 yum install httpd 安装 service httpd start 启动服务 测试是否 启动 I ...
- HTML5 canvas游戏工作原理
HTML5已经不是一个新名词.它看上去很cool,有很多feature,大多数人普遍看好它的发展.对于我来说,最感兴趣的是它的canvas标签,可以结合Javascript来绘制游戏画面. 我们可以在 ...
- Scrapy学习笔记(5)-CrawlSpider+sqlalchemy实战
基础知识 class scrapy.spiders.CrawlSpider 这是抓取一般网页最常用的类,除了从Spider继承过来的属性外,其提供了一个新的属性rules,它提供了一种简单的机制,能够 ...
- Vue基础进阶 之 Vue生命周期与钩子函数
Vue生命周期 Vue生命周期:Vue实例从创建到销毁的过程,称为Vue的生命周期: Vue生命周期示意图:https://cn.vuejs.org/v2/guide/instance.html#生命 ...