题目大意

如果把字符串a重复m次可以得到字符串b,那么我们称字符串a为字符串b的一个因子,现在给定两个字符串S1和S2,求它们的公共因子个数

题解

如果它们有公共因子,那么显然它们的最小公共因子肯定是相等的~~~,公因子就是字符串的最短循环节~~~所以我们先把两个最短循环节给求出来,并判断是否相同,如果相同的话就是它们的最小公因子,然后所有的最小公因子的倍数并且是S1和S2的公约数都是它们的公因子

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <string>
using namespace std;
string T,s1,s2;
int f[100005];
void getfail(string s)
{
int j,len=s.length();
f[0]=j=-1;
for(int i=1;i<len;i++)
{
while(j>=0&&s[j+1]!=s[i]) j=f[j];
if(s[j+1]==s[i]) j++;
f[i]=j;
}
}
int main()
{
int a,b;
cin>>s1>>s2;
int n=s1.length(),m=s2.length();
getfail(s1);
if(n%(n-1-f[n-1])==0)a=n-1-f[n-1];
else a=n;
getfail(s2);
if(m%(m-1-f[m-1])==0)b=m-1-f[m-1];
else b=m;
if(a==b)
{
for(int i=0;i<a;i++)
if(s1[n-i-1]!=s2[m-i-1])
{
printf("0\n");
return 0;
}
int ans=0,t=a,ll=min(n,m);
while(t<=ll)
{
if(n%t==0&&m%t==0) ans++;
t+=a;
}
printf("%d\n",ans);
}
else
printf("0\n");
return 0;
}

Codeforces182D - Common Divisors(KMP)的更多相关文章

  1. Common Divisors CodeForces - 182D || kmp最小循环节

    Common Divisors CodeForces - 182D 思路:用kmp求next数组的方法求出两个字符串的最小循环节长度(http://blog.csdn.net/acraz/articl ...

  2. CF #579 (Div. 3) C.Common Divisors

    C.Common Divisors time limit per test2 seconds memory limit per test256 megabytes inputstandard inpu ...

  3. B - Common Divisors (codeforces)数论算法基本定理,唯一分解定理模板

    You are given an array aa consisting of nn integers. Your task is to say the number of such positive ...

  4. [gcd]Codeforces Common Divisors

    Common Divisors time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  5. Codeforces Round #579 (Div. 3) B Equal Rectangles、C. Common Divisors

    B Equal Rectangles 题意: 给你4*n个数,让你判断能不能用这个4*n个数为边凑成n个矩形,使的每个矩形面积相等 题解: 原本是想着用二分来找出来那个最终的面积,但是仔细想一想,那个 ...

  6. Codeforces Round #117 (Div. 2) D.Common Divisors(KMP最小循环节)

    http://codeforces.com/problemset/problem/182/D 题意:如果把字符串a重复m次可以得到字符串b,那么我们称字符串a为字符串b的一个因子,现在给定两个字符串S ...

  7. <Sicily>Greatest Common Divisors

    一.题目描述 A common divisor for two positive numbers is a number which both numbers are divisible by. It ...

  8. A题:Common Substrings(KMP应用)

    原题链接 注意:2号和3号get_next()函数中next[i]赋值时的区别,一个是0,一个是1,且不能互换 #include<cstdio> #include<cstring&g ...

  9. D - Disjoint Set of Common Divisors

    https://atcoder.jp/contests/abc142/tasks/abc142_d 题意 求满足互素条件下的A和B的因子最多有几个 思路: 分解gcd(A,B)的质因子,再加上1: # ...

随机推荐

  1. 信号量 sem_undo设置

    一 为什么要使用信号量 为了防止出现因多个程序同时访问一个共享资源而引发的一系列问题,我们需要一种方法,它可以通过生成并使用令牌来授权,在任一时刻只能有一个执行线程访问 代码的临界区域.临界区域是指执 ...

  2. git reflog 和git log :no branch git 提交方式

    git reflog 和git log的区别,外加git cherry-pick的一种用法 git reflog 可以查看所有分支的所有操作记录(包括(包括commit和reset的操作),包括已经被 ...

  3. ASP.Net 添加 Interop for Word, excel 插件

    1:在服务器上安装office的Excel软件. 2:在"开始"->"运行"中输入dcomcnfg.exe启动"组件服务" 3:依次双 ...

  4. r个有标志的球放进n个不同的盒子里,要求无一空盒,问有多少种不同的分配方案?

           由题意可知道r>=n,我原来想的是先取n个全排列,剩下的r-n个每个有n中选择,所以结果是n!*n^(r-n).经满神猜测,这样是会重复的.比如说,1到5个球,ABC三个盒子,ms ...

  5. 解读分库分表中间件Sharding-JDBC

    [编者按]数据库分库分表从互联网时代开启至今,一直是热门话题.在NoSQL横行的今天,关系型数据库凭借其稳定.查询灵活.兼容等特性,仍被大多数公司作为首选数据库.因此,合理采用分库分表技术应对海量数据 ...

  6. jquery data方法

    jquery.data()文档:http://api.jquery.com/jQuery.data/ html5有个data-*属性,跟这个功能一样. Note: This is a low-leve ...

  7. Java-Swing嵌入浏览器(二)

    这是qtjambi利用webview来做嵌入式浏览器,下面是我的工程目录. 运行效果如下图: 代码相关: package qtBowers; import com.trolltech.qt.core. ...

  8. tomcat docBase 和 path

    <Context docBase="zjzc-web-api" path="/api" reloadable="false"/> ...

  9. 个人所得税计算器2016 by Jacksile

    个人所得税计算器2016 // (83500+i)) { var to=(all*45/100-13505).toFixed(2); document.getElementById("int ...

  10. BZOJ_3270_博物馆_(高斯消元+期望动态规划+矩阵)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=3270 \(n\)个房间,刚开始两个人分别在\(a,b\),每分钟在第\(i\)个房间有\(p[ ...