浅谈\(KMP\):https://www.cnblogs.com/AKMer/p/10438148.html

题目传送门:http://poj.org/problem?id=1961

根据研究发现,如果一个字符串可以被若干个字符串首尾相连拼接而成,那么必然存在\(nxt_n\ne0,n\ mod\ (n-nxt_n)=0\)

求一遍\(nxt\)数组就行了。

时间复杂度:\(O(n)\)

空间复杂度:\(O(n)\)

代码如下:

#include <cstdio>
using namespace std; const int maxn=1e6+5; int n;
char s[maxn];
int nxt[maxn]; int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} void make_nxt() {
for(int j=0,i=2;i<=n;i++) {
while(j&&s[j+1]!=s[i])j=nxt[j];
if(s[j+1]==s[i])j++;nxt[i]=j;
}
} int main() {
int Test=0;
while(1) {
n=read();if(!n)break;
printf("Test case #%d\n",++Test);
scanf("%s",s+1),make_nxt();
for(int i=2;i<=n;i++)
if(nxt[i]&&i%(i-nxt[i])==0)
printf("%d %d\n",i,i/(i-nxt[i]));
puts("");
}
return 0;
}

POJ1961:Period的更多相关文章

  1. 「UVA1328」「POJ1961」 Period 解题报告

    UVA1328 Period 其他链接:luogu UVA1328 POJ1961 For each prefix of a given string S with N characters (eac ...

  2. 【POJ1961】period

    [POJ1961]period 题目描述 如果一个字符串S是由一个字符串T重复K次构成的,则称T是S的循环元.使K出现最大的字符串T称为S的最小循环元,此时的K称为最大循环次数. 现在给定一个长度为N ...

  3. poj1961 & hdu1358 Period【KMP】

    Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 20436   Accepted: 9961 Descripti ...

  4. POJ 1961:Period

    Period Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 14280 Accepted: 6773 Description F ...

  5. HDU1358:Period

    第一次做KMP.还没有理解透. 在自己写一遍时没有让next[0]初始化为-1. 还有就是next应该是c++中的关键字,提交后编译错误. From: http://blog.csdn.net/lib ...

  6. UVALive - 3026:Period

    用KMP里面的next数组即可,原理就是next数组的原理 #include<cstdio> #include<cstdlib> #include<algorithm&g ...

  7. 【暑假】[实用数据结构]UVAlive 3026 Period

    UVAlive 3026 Period 题目: Period   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld ...

  8. R语言学习 第十一篇:日期和时间

    R语言的基础包中提供了三种基本类型用于处理日期和时间,Date用于处理日期,它不包括时间和时区信息:POSIXct/POSIXlt用于处理日期和时间,其中包括了日期.时间和时区信息.R内部在存储日期和 ...

  9. 【源码解读】EOS测试插件:txn_test_gen_plugin.cpp

    本文内容本属于<[精解]EOS TPS 多维实测>的内容,但由于在编写时篇幅过长,所以我决定将这一部分单独成文撰写,以便于理解. 关键字:eos, txn_test_gen_plugin, ...

随机推荐

  1. JavaEE之Junit单元测试

    1编写测试类,简单理解Junit可以部分用于取代java的main方法 2在测试类方法上添加注解 @Test 3 @Test修饰的方法要求:public void 方法名() {…} ,方法名自定义建 ...

  2. LeetCode——Max Consecutive Ones

    LeetCode--Max Consecutive Ones Question Given a binary array, find the maximum number of consecutive ...

  3. Hive-开启动态分区

    开启动态分区 --开启动态分区 set hive.exec.dynamic.partition=true; set hive.exec.dynamic.partition.mode=nonstrict ...

  4. oracle 结构化语言查询 DML DDL DCL

    --结构化查询语言 (Structured Query Language),具有定义. --查询.更新和控制等多种功能,是关系数据库的标准语言. --SQL分类: -- 数据操纵语言DML Data ...

  5. 把本地jar包发布到maven私服和本地maven库

    有时时候下载了jar包,但发现maven库里没有,可以将jar包上传到本地私服和本地maven库: 1.上传到本地私服 mvn deploy:deploy-file -Dfile=D:\GETUI_S ...

  6. TUNING FOR ALL FLASH DEPLOYMENTS

    Ceph Tuning and Best Practices for All Flash Intel® Xeon® ServersLast updated: January 2017 TABLE OF ...

  7. Educational Codeforces Round 27

    期末后恢复性训练,结果完美爆炸... A,题意:2n个人,分成两队,要求无论怎么分配,第一队打赢第二队 #include<bits/stdc++.h> #define fi first # ...

  8. 使用cqlsh远程连接cassandra——设置cassandra.yaml里rpc_address和listen_address为ipv4地址即可

    You need to edit cassandra.yaml on the node you are trying to connect to and set the node ip address ...

  9. hdu4619

    题解: 最大独立集问题 显然对于每一对交叉的建边 然后求出最大独立集 最大独立集=n-最大匹配 代码: #include<cstdio> #include<cmath> #in ...

  10. 2017.11.18 IAP下载(STM8,PIC,STM32)

    客户要求用IAP下载,mark一下,客户还给了stm32的引导码.仅供参考. 1 PIC单片机的IAP  2 STm32 IAP https://www.cnblogs.com/WeyneChen/p ...