UVA 1328 - Period KMP
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36131
题意:给出一个长度为n的字符串,要求找到一些i,满足说从1~i为多个个的重复子串构成,并输出子串的个数。
题解:对kmp中预处理的数组的理解
//作者:1085422276
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include<bits/stdc++.h>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
const int inf = ;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
ll temp,p;
if(b==)
{
x=;
y=;
return a;
}
p=exgcd(b,a%b,x,y);
temp=x;
x=y;
y=temp-(a/b)*y;
return p;
}
//*******************************
int n,p[];char a[];
int main()
{
int oo=;
while(scanf("%d",&n)!=EOF)
{
if(n==)break;
scanf("%s",a+);
memset(p,,sizeof(p));
int j=;
for(int i=;i<=n;i++)
{
while(j>&&a[j+]!=a[i])j=p[j];
if(a[j+]==a[i])j++;
p[i]=j;
}
cout<<"Test case #"<<oo++<<endl;
for(int i=;i<=n;i++)
{
if(p[i]>&&i%(i-p[i])==){
cout<<i<<" "<<i/(i-p[i])<<endl;
}
}
cout<<endl;
}
return ;
}
代码
UVA 1328 - Period KMP的更多相关文章
- UVA - 1328 Period(循环节kmp)
https://vjudge.net/problem/UVA-1328 题意 求每个前缀的最小循环节,要求至少循环两次且为完整的. 分析 求next数组,i-next[i]即为前缀i的最小循环节,再判 ...
- UVa 1328 Period
数据范围较大,故用KMP求循环节 之后由小到大枚举长度范围,若该长度下有循环节就输出答案 还要注意输出格式.之前测试时候连着一串presentation error也是悲伤 #include<b ...
- UVa 1328 (KMP求字符串周期) Period
当初学KMP的时候也做过这道题,现在看来还是刘汝佳的代码要精简一些,毕竟代码越短越好记,越不容易出错. 而且KMP的递推失配函数的代码风格和后面的Aho-Corasick自动机求失配函数的代码风格也是 ...
- POJ 1961 Period( KMP )*
Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...
- hdu oj Period (kmp的应用)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- hdu 1358 period KMP入门
Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...
- LA3026 - Period(KMP)
For each prefix of a given string S with N characters (each character has an ASCII code between 97 a ...
- poj1961 Period kmp解决找字符串的最小循环节
/** 题目:poj1961 Period 链接:http://poj.org/problem?id=1961 题意:求从1到i这个前缀(2<=i<=N) ,如果有循环节(不能自身单独一个 ...
- HDU1358 Period —— KMP 最小循环节
题目链接:https://vjudge.net/problem/HDU-1358 Period Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
随机推荐
- Component creation must be done on Event Dispatch Thread错误解决方法
在用java swing 做例子,给页面设置皮肤样式的时候出现了这个错误: org.jvnet.substance.api.UiThreadingViolationException: Compone ...
- 登录DA面板出现:License has expired
登录DA面板出现:License has expired的解决方法. 首先看是否过期,如果出现The license looks fine on this end. 登录 SSH as root # ...
- iOS7: 如何获取不变的UDID
如何使用KeyChain保存和获取UDID 本文是iOS7系列文章第一篇文章,主要介绍使用KeyChain保存和获取APP数据,解决iOS7上获取不变UDID的问题.并给出一个获取UDID的工具类,使 ...
- poj.2419.Forests (枚举 + set用法)
Forests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5782 Accepted: 2218 Descripti ...
- PHP学习之一晚撸下W3chscool
PHP 多维数组 其实简单的而言,多维数组就是由单个的数组组成的,两个数组嵌套组成一个二维数组,三个顾名思义就是三维数组. 先来一个简单的数组. 数字是key,引号里的是value <?php ...
- C#父类子类对象关系
案例: 主要有Vehicle.cs Airplane.cs Car.cs 3个类. Car和Airplane都继承与Vehicle类.Vehicle中Drive为虚方法,可在子类中重写,父类引 ...
- tomcat安全配置(二)
1. JVM 1.1. 使用 Server JRE 替代JDK. 服务器上不要安装JDK,请使用 Server JRE. 服务器上根本不需要编译器,代码应该在Release服务器上完成编译打包工作. ...
- FOJ 1075
#include<stdio.h> #include<iostream> using namespace std; ]={,}; ]={}; void func() { ,fl ...
- Redis和Memcache的区别分析
1. Redis中,并不是所有的数据都一直存储在内存中的,这是和Memcached相比一个最大的区别. 2. Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,hash等数据结构 ...
- python string与list互转
因为python的read和write方法的操作对象都是string.而操作二进制的时候会把string转换成list进行解析,解析后重新写入文件的时候,还得转换成string. >>&g ...