题目大意

如果把字符串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. AMH4.2免费版手动编译升级Nginx1.8版本方法

    从AMH免费版本停留在4.2版本之后就没有进行更新和升级,而且官方提供的解决文档也比较少,毕竟免费且没有盈利的产品还是没有多少兴趣的.但是,对于大部分网站环境来说,安装和使用AMH4.2免费版本还是够 ...

  2. Hadoop 2.4.0完全分布式平台搭建、配置、安装

    一:系统安装与配置 Hadoop选择下载2.4.0 http://hadoop.apache.org / http://mirror.bit.edu.cn/apache/hadoop/common/h ...

  3. RubyCritic:一款不错的检测代码质量工具

    关注代码质量是高效开发必须要做的一件事,那么在 Ruby 开发的过程中,是否有什么好的代码质量检测工具呢?下面由 Ruby 工程师路英瑞介绍一下 RubyCritic--一款还不错的代码质量检测工具. ...

  4. JavaScript 弹出窗体点击按钮返回选择数据的实现

    首先是父页面的代码: <head runat="server"> <title>无标题页</title> <%-- <script ...

  5. php stdClass Object 问题

    Array ( [0] => stdClass Object ( [term_id] => 3 [name] => apache [slug] => apache [term_ ...

  6. Objective-c Category(类别)

    NSStringUtilities.h: #import <Foundation/Foundation.h> @interface NSString(Utilities) -(BOOL) ...

  7. PHP ‘scan’函数拒绝服务漏洞

    漏洞名称: PHP ‘scan’函数拒绝服务漏洞 CNNVD编号: CNNVD-201311-464 发布时间: 2013-12-06 更新时间: 2013-12-06 危害等级: 中危   漏洞类型 ...

  8. -_-#【Cookie】使用无 cookies 的域

    cookie-free域名提高网页效率

  9. c语言字符串库函数#include<string.h>

    字符串函数<string.h> 在头文件<string.h>中定义了两组字符串函数.第一组函数的名字以str开头:第二组函数的名字以mem开头.只有函数memmove对重叠对象 ...

  10. wcf 多个节点名出错

    无法加载协定为“ServiceReference1.xxxxxx”的终结点配置部分,因为找到了该协定的多个终结点配置.请按名称指示首选的终结点配置部分. 原因是config节点中多了endpoint相 ...