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/ ...
随机推荐
- 大数据和hadoop的一些基础知识
一.前言 大数据这个概念不用我提大家也听过很多了,前几年各种公开论坛.会议等场合言必及大数据,说出来显得很时髦似的.有意思的是最近拥有这个待遇的名词是“人工智能/AI”,当然这是后话. 众所周知,大数 ...
- PyCharm2017破解步骤
前段时间买了一套python的学习视频,附带一个Pycharm的安装包和注册码,现在注册码被JetBrains封杀了,不得已在网上找了一下破解的教程,在这里记录一下: 先找到破解补丁无需使用注册码,下 ...
- MongoDB遇到的疑似数据丢失的问题。不要用InsertMany!
最近做数据备份的时候发现了有个很严重的问题,那就是数据丢失(最后证明没丢,是别的问题造成的). 问题如下: 我通过两种方式在两个mongoDB集群中,对一组collection进行备份,最后2个备份数 ...
- centos下搭建Jenkins持续集成环境(安装jenkins)
1.安装JDK yum install -y java 2.安装jenkins 添加Jenkins库到yum库,Jenkins将从这里下载安装. 1 wget -O /etc/yum.repos.d/ ...
- 介绍Python中6个序列的内置类型
1.Python中6个序列的内置类型分别是什么? Python包含6中内建的序列,即列表.元组.字符串.Unicode字符串.buffer对象和 xrange 对象.序列通用的操作包括:索引.长度.组 ...
- js获取浏览器类型和版本信息
bro () { let broName = 'Runing' let strStart = 0 let strStop = 0 let temp = '' let userAgent = windo ...
- 怎样从外网访问内网Jboss?
本地安装了一个Jboss,只能在局域网内访问,怎样从外网也能访问到本地的Jboss呢?本文将介绍具体的实现步骤. 准备工作 安装并启动Jboss 默认安装的Jboss端口是8080. 实现步骤 下载并 ...
- javaweb项目中errorPage的问题
我们的请求找不到时,会跳到错误页面,tomcat提供了一个错误页面,但是不太好.分析:tomcat自带错误页面不好的原因:有一下两点: 1.不好看: 2.不能为seo做出贡献.思考:如何解决以上问题? ...
- 判断闰年C语言版
#include<stdio.h> int isLeap(int year) { // 必须先判断是平年的情况 后判断闰年的情况 == && year%!=) || yea ...
- Eloquent JavaScript #10# Modules
索引 Notes 背景问题 模块Modules 软件包Packages 简易模块 Evaluating data as code CommonJS modules ECMAScript modules ...