两道题都是求循环节的。。。但是一道是学哈希时做的,另一道是学$KMP$时做的


POJ2604 用的哈希。。。枚举长度的因数作为循环节的长度,然后暴力算出所有循环节位置的哈希值,看看是否相等。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<vector>
#include<queue>
#include<map>
#include<set>
#define ll unsigned long long
#define R register int
using namespace std;
namespace Fread {
static char B[<<],*S=B,*D=B;
#define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
inline int g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
}
}using Fread::g;
const int B=,N=;
ll f[N],p[N];
inline ll calc(int l,int r) {return f[r]-f[l-]*p[r-l+];}
signed main() {
#ifdef JACK
freopen("NOIPAK++.in","r",stdin);
#endif
p[]=; for(R i=;i<=N-;++i) p[i]=p[i-]*B;
while() { register char s[N];
scanf("%s",s+); R n=strlen(s+);
if(n==&&s[]=='.') break;
for(R i=;i<=n;++i) f[i]=f[i-]*B+s[i]-;
for(R i=;i<=n;++i) if(n%i==) { register ll tmp=calc(,i);
register bool flg=false;
for(R l=i+;l<=n;l+=i) {
if(tmp!=calc(l,l+i-)) {flg=; break;}
} if(!flg) {printf("%d\n",n/i);break;}
}
}
}

POJ1961 用的$kmp$详解

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<vector>
#include<queue>
#include<map>
#include<set>
#define ull unsigned long long
#define ll long long
#define R register int
using namespace std;
namespace Fread {
static char B[<<],*S=B,*D=B;
#define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
inline int g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
} inline bool isempty(const char& ch) {return ch<=||ch>=;}
inline void gs(char* s) {register char ch; while(isempty(ch=getchar())); do *s++=ch; while(!isempty(ch=getchar()));}
}using Fread::g; using Fread::gs;
const int N=;
char s[N]; int nxt[N],n,t;
inline void PRE() { nxt[]=;
for(R i=,j=;i<=n;++i) {
while(j&&s[i]!=s[j+]) j=nxt[j];
if(s[i]==s[j+]) ++j; nxt[i]=j;
}
}
signed main() {
#ifdef JACK
freopen("NOIPAK++.in","r",stdin);
#endif
while(n=g(),n>) {
gs(s+); PRE(); printf("Test case #%d\n",++t);
for(R i=;i<=n;++i) {
if(i%(i-nxt[i])==&&i/(i-nxt[i])>)
printf("%d %d\n",i,i/(i-nxt[i]));
} printf("\n");
}
}

2019.06.27

POJ1961 Period && POJ2604 Power Strings 字符串循环节的更多相关文章

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

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

  2. UVA 10298 Power Strings 字符串的幂(KMP,最小循环节)

    题意: 定义a为一个字符串,a*a表示两个字符相连,即 an+1=a*an ,也就是出现循环了.给定一个字符串,若将其表示成an,问n最大为多少? 思路: 如果完全不循环,顶多就是类似于abc1这样, ...

  3. UVA - 10298 Power Strings (KMP求字符串循环节)

    Description Problem D: Power Strings Given two strings a and b we define a*b to be their concatenati ...

  4. POJ:2406-Power Strings(寻找字符串循环节)

    Power Strings Time Limit: 3000MS Memory Limit: 65536K Description Given two strings a and b we defin ...

  5. POJ:1961-Period(寻找字符串循环节)

    Period Time Limit: 3000MS Memory Limit: 30000K Description For each prefix of a given string S with ...

  6. POJ2406-Power Strings(kmp循环节)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31111   Accepted: 12982 D ...

  7. UVA1328 Period【KMP/周期串/循环节】By cellur925

    鲜有的在luogu发题解以及使用LaTex??? 就丢链接跑了.

  8. - Power Strings (字符串哈希) (KMP)

    https://www.cnblogs.com/widsom/p/8058358.htm (详细解释) //#include<bits/stdc++.h> #include<vect ...

  9. POJ 2406 Power Strings(字符串的最小循环节)

    题目链接:http://poj.org/problem?id=2406 题意:确定字符串最多是多少个相同的字串重复连接而成的 思路:关键是找到字符串的最小循环节 code: #include < ...

随机推荐

  1. LeetCode-第 166 场周赛

    LeetCode-第 166 场周赛 1281.subtract-the-product-and-sum-of-digits-of-an-integer 1282.group-the-people-g ...

  2. 1.3Security:权限管理,过滤、监听、拦截

    Security:权限管理 常用权限拦截器 SecurityContextPersistenceFilter 以前是HttpSesstionContextIntegrationFilter,位于过滤器 ...

  3. HDU 3642 求体积交集

    Get The Treasury 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3642 Problem Description Jack knows ...

  4. python字典改变value值方法总结

    今天这篇文章中我们来了解一下python之中的字典,在这文章之中我会对python字典修改进行说明,以及举例说明如何修改python字典内的值.我们开始进入文章吧. 首先我们得知道什么是修改字典 修改 ...

  5. linux下shell 脚本 中windows换行符换成linux换行符

    sed -i 's/\r//' filename window下默认是 \r\n linux下是\n unix下是\r

  6. 第十三章 ZYNQ-MIZ701 TIMER定时器中断

      上篇文章实现了了PS接受来自PL的中断,本片文章将在ZYNQ的纯PS里实现私有定时器中断.每隔一秒中断一次,在中断函数里计数加1,通过串口打印输出. 本文所使用的开发板是Miz701 PC 开发环 ...

  7. 基于白名单的Payload

    利用 Msiexec 命令DLL反弹 Msiexec是Windows Installer的一部分.用于安装Windows Installer安装包(MSI),一般在运行Microsoft Update ...

  8. LeetCode 899. Orderly Queue

    899. Orderly Queue(有序队列) 题目: 给出了一个由小写字母组成的字符串 S.然后,我们可以进行任意次数的移动. 在每次移动中,我们选择前 K 个字母中的一个(从左侧开始),将其从原 ...

  9. script标签所应放的位置

    一般放置的位置:<head>标签内,<body>标签内,<body>标签后(建议放在body标签后,利于页面的优化,优化页面结构加载的速度) 1.<head& ...

  10. [转]关闭ssh的自动启动

    转载自:http://blog.chinaunix.net/uid-20147410-id-3206364.html 安装了ssh服务,但是不希望他开机自动启动,可以如下设置: 在/etc/init/ ...