Codeforces182D - Common Divisors(KMP)
题目大意
如果把字符串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)的更多相关文章
- Common Divisors CodeForces - 182D || kmp最小循环节
Common Divisors CodeForces - 182D 思路:用kmp求next数组的方法求出两个字符串的最小循环节长度(http://blog.csdn.net/acraz/articl ...
- CF #579 (Div. 3) C.Common Divisors
C.Common Divisors time limit per test2 seconds memory limit per test256 megabytes inputstandard inpu ...
- B - Common Divisors (codeforces)数论算法基本定理,唯一分解定理模板
You are given an array aa consisting of nn integers. Your task is to say the number of such positive ...
- [gcd]Codeforces Common Divisors
Common Divisors time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #579 (Div. 3) B Equal Rectangles、C. Common Divisors
B Equal Rectangles 题意: 给你4*n个数,让你判断能不能用这个4*n个数为边凑成n个矩形,使的每个矩形面积相等 题解: 原本是想着用二分来找出来那个最终的面积,但是仔细想一想,那个 ...
- Codeforces Round #117 (Div. 2) D.Common Divisors(KMP最小循环节)
http://codeforces.com/problemset/problem/182/D 题意:如果把字符串a重复m次可以得到字符串b,那么我们称字符串a为字符串b的一个因子,现在给定两个字符串S ...
- <Sicily>Greatest Common Divisors
一.题目描述 A common divisor for two positive numbers is a number which both numbers are divisible by. It ...
- A题:Common Substrings(KMP应用)
原题链接 注意:2号和3号get_next()函数中next[i]赋值时的区别,一个是0,一个是1,且不能互换 #include<cstdio> #include<cstring&g ...
- D - Disjoint Set of Common Divisors
https://atcoder.jp/contests/abc142/tasks/abc142_d 题意 求满足互素条件下的A和B的因子最多有几个 思路: 分解gcd(A,B)的质因子,再加上1: # ...
随机推荐
- online web design tool
https://webflow.com/ http://css-tricks.com/snippets/ https://www.fluidui.com/demos
- 用原生JavaScript实现图片瀑布流的浏览效果
学习JS,活跃思维,灵活运用的一个较为典型的学习案例.同一个瀑布流的效果但实现方式却很多,利用递归.冒泡等等手法都可以达到你想要的目的.这次要说的就是利用类似递归来实现此效果的原创方案.此方案个人认为 ...
- [CC150] Get all permutations of a string
Problem: Compute all permutations of a string of unique characters. 此题用循环的方式不好做,下面是一种递归的思路: 把给的字符串看成 ...
- COOKIE之安全设置漫谈
一.标题:COOKIE之安全设置漫谈 副标:httponly属性和secure属性解析 二.引言 经常有看到XSS跨站脚本攻击窃取cookie案例,修复方案是有httponly.今天写出来倒腾下... ...
- 用PYTHON硬写SOCKET
这个文章的系列很有意思,练练~~: http://python.jobbole.com/82763/ :) 第一步,最简单的SERVER和CLIENT.感觉和写JAVA,C的最简单的一样一样的,,看来 ...
- Jmeter java协议配置文件导入
一. 方法一 通过ClassLoader获取当前路径,想在java脚本里读取druid.properties,路径如下 apache-jmeter --bin --druid.properties 相 ...
- JAVA三大框架的各自作用
http://christhb.blog.163.com/blog/static/98982492011727114936239/ 一.Spring Spring是一个解决了许多在J2EE开发中常见的 ...
- javaweb学习总结(四十一)——Apache的DBUtils框架学习
一.commons-dbutils简介 commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化 ...
- SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-004-Pizza例子的用户流程(flowExecutionKey、_eventId_phoneEntered、flowExecutionUrl )
一. 1. 2. 3.customer-flow.xml 自己定义customer,最后output <?xml version="1.0" encoding="U ...
- 转:HTTP请求(GET、POST和soap区别)和响应
一直对Http请求和SOAP请求不是太理解,只是知道SOAP是基于Http的,并且增加了很多XML标签,SOAP经常用在WebService中,比如在C#中创建一个WebService,然后在客户端生 ...