题目大意

如果把字符串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. 如何根据w3wp.exe的进程pid查看是哪个应用程序池

    如何根据w3wp.exe的进程pid查看是哪个应用程序池? 根据iisapp 查看PID所对应的IIS应用程序池及详细介绍: 从IIS6.0可以在IIS中架设多个站点并给每个站点指定不同的应用程序池, ...

  2. [CC150] 八皇后问题

    Write an algorithm to print all ways of arranging eight queens on an 8*8 chess board so that none of ...

  3. ExtJS 获取浏览器宽度

    JS中代码: Ext.onReady(function() { var width=window.screen.availWidth; var height=window.screen.availHe ...

  4. Oracle----date

    to_date('december 31, 2008, 11:30 p.m.', 'Month dd, YYYY, HH:MI P.M.')

  5. 转载:redis备份策略

    Redis提供了两种持久化选项,分别是RDB和AOF. 默认情况下60秒刷新到disk一次[save 60 10000 当有1w条keys数据被改变时],Redis的数据集保存在叫dump.rdb一个 ...

  6. Android EditText的常用技巧

    1.       设定 EditText 的滚动条.对齐方式.行数.和提示 (hint) 及其颜色 在布局文件,比如 main.xml 中,增加 < EditText android:id =  ...

  7. Android进阶篇-时间滑动控件

    仿Iphone时间选择滑动控件: WheelView.java: /** * @author Administrator * * 时间滑动滚轮 */ public class WheelView ex ...

  8. [译]GotW #6b Const-Correctness, Part 2

         const和mutable对于书写安全代码来说是个很有利的工具,坚持使用它们. Problem Guru Question 在下面代码中,在只要合适的情况下,对const进行增加和删除(包括 ...

  9. 使用智遥工作流,优化SAP请购流程

    传统请购流程,都是用户在SAP系统中填写请购单,然后再打印出来,递交给上级领导审批.领导审批完了,再到SAP系统中更新release标识.若中途请购单内容需要变更,则需要重新打印,审批. 智遥工作流, ...

  10. URAL1012. K-based Numbers. Version 2

    链接 考查大数 正好拿来学习下JAVA JAVA好高端.. import java.io.*; import java.math.*; import java.text.*; import java. ...