题目链接: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的更多相关文章

  1. UVA - 1328 Period(循环节kmp)

    https://vjudge.net/problem/UVA-1328 题意 求每个前缀的最小循环节,要求至少循环两次且为完整的. 分析 求next数组,i-next[i]即为前缀i的最小循环节,再判 ...

  2. UVa 1328 Period

    数据范围较大,故用KMP求循环节 之后由小到大枚举长度范围,若该长度下有循环节就输出答案 还要注意输出格式.之前测试时候连着一串presentation error也是悲伤 #include<b ...

  3. UVa 1328 (KMP求字符串周期) Period

    当初学KMP的时候也做过这道题,现在看来还是刘汝佳的代码要精简一些,毕竟代码越短越好记,越不容易出错. 而且KMP的递推失配函数的代码风格和后面的Aho-Corasick自动机求失配函数的代码风格也是 ...

  4. POJ 1961 Period( KMP )*

    Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...

  5. hdu oj Period (kmp的应用)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  6. hdu 1358 period KMP入门

    Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...

  7. LA3026 - Period(KMP)

    For each prefix of a given string S with N characters (each character has an ASCII code between 97 a ...

  8. poj1961 Period kmp解决找字符串的最小循环节

    /** 题目:poj1961 Period 链接:http://poj.org/problem?id=1961 题意:求从1到i这个前缀(2<=i<=N) ,如果有循环节(不能自身单独一个 ...

  9. HDU1358 Period —— KMP 最小循环节

    题目链接:https://vjudge.net/problem/HDU-1358 Period Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

随机推荐

  1. c语言的头文件-不是c++类的头文件?

    下面的概述是参考的这篇文章:http://blog.csdn.net/bingxx11/article/details/7771437 c语言编程中也有,也需要头文件, 头文件不只是C++的类才需要! ...

  2. We are 歪果仁带你灰

    We are 歪果仁带你灰 我叫赖彦谕 不爱什么诗和远方 只爱饭桌上的一菜一肉一杯酒 真的很希望有一天我可以成为那个对待学习像对待美食一样的人类 让自己沉浸在美食和知识的海洋中无法自拔 我也对未来的软 ...

  3. [Effective JavaScript 笔记]第32条:始终不要修改__proto__属性

    __proto__属性很特殊,它提供了Object.getPrototypeOf方法所不具备的额外能力,即修改对象原型链接的能力. 避免修改__proto__属性的最明显的原因是可移植性的问题.并不是 ...

  4. Unity 3D 粒子系统的一点经验

    http://hunterwang.diandian.com/post/2012-10-21/40041523890 最近做东西需要增加效果,简单的运用了一下粒子效果,真心感觉比较难调整好效果.同时也 ...

  5. 滚动光效shader

    Shader "Custom/LightMove" { Properties { _MainTex ("Base (RGB)", 2D) = "whi ...

  6. cocos2d 如何优化内存使用

    如何优化内存使用 内存优化原理 为优化应用内存使用,开发人员首先应该知道什么最耗应用内存,答案就是纹理! 纹理几乎会占据90%应用内存.所以尽量最小化应用的纹理内存使用,否则应用很有可能会因为低内存而 ...

  7. Access数据库之偏移注入

    /*转载请注明出处:珍惜少年时*/ 偏移注入主要是针对知道表,但是不知道字段的. 这里我已经知道了表明是:sys_admin 可以使用: select exists(selct * from sys_ ...

  8. sql注入攻击的预防函数

    /* 待更新 */ addslashes  htmlspecialchars  mysql_escape_string($string) mysql_real_escape_string($strin ...

  9. nyoj19 全排列

    http://acm.nyist.net/JudgeOnline/status.php?pid=19 #include<stdio.h> #include<stdlib.h> ...

  10. 部署Office Web Apps Server并配置其与SharePoint 2013的集成

    部署Office Web Apps Server并配置其与SharePoint 2013的集成   Office Web Apps Server 是新的 Office 服务器产品,它提供 Word.P ...