两道题都是求循环节的。。。但是一道是学哈希时做的,另一道是学$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. Python实现二叉树的非递归先序遍历

    思路: 1. 使用列表保存结果: 2. 使用栈(列表实现)存储结点: 3. 当根结点存在,保存结果,根结点入栈: 4. 将根结点指向左子树: 5. 根结点不存在,栈顶元素出栈,并将根结点指向栈顶元素的 ...

  2. 三种SpringSecurity方法级别权限控制

    一 JSR-250注解 1.在pom.xml添加 <dependency> <groupId>javax.annotation</groupId> <arti ...

  3. B - How many integers can you find

      Now you get a number N, and a M-integers set, you should find out how many integers which are smal ...

  4. [Vue]vue-router的push和replace的区别

    1.this.$router.push() 描述:跳转到不同的url,但这个方法会向history栈添加一个记录,点击后退会返回到上一个页面. 2.this.$router.replace() 描述: ...

  5. java方法可变参数研究

    1 问题引出 (1)缘由 最近在研究如何在项目中引入Redis缓存,于是遇到可变参数这个疑惑点,之前没有好好研究过,为了避免项目后期出现问题. (2)项目相关技术 SpringBoot Redis K ...

  6. C语言两个特别大的整数类型相加超出范围使用两个技巧

    技巧1:用long (%ld)或者long long(%lld)类型存取 技巧2:当两个同号的数字相加,放到等号的另一边,变成减号 问题: 给定区间[-2的31次方, 2的31次方]内的3个整数A.B ...

  7. Azure下安装Redis

    注意:这里需要注意Redis的区域,需要跟服务器在同一区域,否则Redis访问会很慢 Azure 中国区目前不支持在Portal管理界面创建 Redis,只能通过PowerShell创建,请参考以下步 ...

  8. [NOIP10.4模拟赛]3.z题解--思维

    题目链接: 咕咕 闲扯: 哈哈这道T3考场上又敲了5个namespace,300+行,有了前车之鉴还对拍过,本以为子任务分稳了 结果只有30分哈哈,明明用极限数据对拍过不知怎么回事最后数据又是读不全, ...

  9. Swagger学习(二、配置swagger)

    基于上一篇 其实只是在SwaggerConfig.class中配置docket,apiInfo @Configuration //变成配置文件 @EnableSwagger2 //开启swagger2 ...

  10. [转载]SSD原理与实现

    [转载]SSD原理与实现 这里只mark一下,对原论文讲解的很好的博文 https://zhuanlan.zhihu.com/p/33544892 这里有一个关于SSD的很好的程序实现,readme里 ...