POJ 3461: Oulipo

题意:

求出第一个串在第二个串中的出现次数...

分析:

KMP板子题...

代码:

 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
using namespace std;
//眉眼如初,岁月如故 const int maxn=+; int cas,lens,lenp,nxt[maxn]; char s[maxn],p[maxn]; inline void getnxt(void){
nxt[]=nxt[]=;int k;
for(int i=;i<lenp;i++){
k=nxt[i];
while(k&&p[k+]!=p[i+])
k=nxt[k];
if(p[k+]==p[i+])
nxt[i+]=k+;
else
nxt[i+]=;
}
} inline void kmp(void){
int ans=,posp=,poss=;
while(poss<=lens){
if(p[posp]==s[poss])
posp++,poss++;
else if(posp==)
poss++;
else
posp=nxt[posp-]+;
if(posp==lenp+)
ans++,posp=nxt[posp-]+;
}
printf("%d\n",ans);
} signed main(void){
scanf("%d",&cas);
while(cas--){
memset(nxt,,sizeof(nxt));
scanf("%s%s",p+,s+);
lenp=strlen(p+);lens=strlen(s+);
getnxt();kmp();
}
return ;
}//Cap ou pas cap. Pas cap.

POJ 2406: Power Strings

题意:

求出每个串的最短循环节出现次数...

分析:

此题两种解法...然而本质上是一样的...

No.1 Hash

对于一个串其前缀A和后缀B相同并且有重叠,并且len%strlen(str-B)==0,那么str-B一定是循环节...所以我们可以用hash水过...

No.2 next数组...

然而更机智的做法是next数组...

代码:

No.1 Hash

 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define int long long
using namespace std;
const int maxn=+,MOD=;
char str[maxn];
int len,ans;
unsigned int hash[maxn],pow[maxn];
signed main(void){
pow[]=(long long);
for(int i=;i<maxn;i++)
pow[i]=(pow[i-]*)%MOD;
while(scanf("%s",str+)&&str[]!='.'){
len=strlen(str+),hash[]=,ans=;
for(int i=;i<=len;i++)
hash[i]=(hash[i-]*%MOD+str[i]-'a'+)%MOD;
for(int i=;i<len;i++){
if(len%(len-i)==){
if(hash[i]%MOD==(hash[len]-hash[len-i]*pow[i]%MOD+MOD)%MOD){
ans=len/(len-i);
}
}
}
cout<<ans<<endl;
}
return ;
}

No.2 next数组

 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
using namespace std;
//眉眼如初,岁月如故 const int maxn=+; int len,nxt[maxn]; char str[maxn]; inline void getnxt(void){
nxt[]=nxt[]=;int k;
for(int i=;i<len;i++){
k=nxt[i];
while(k&&str[k+]!=str[i+])
k=nxt[k];
if(str[k+]==str[i+])
nxt[i+]=k+;
else
nxt[i+]=;
}
} signed main(void){
while(scanf("%s",str+)&&str[]!='.'){
len=strlen(str+);getnxt();
if(len%(len-nxt[len])==)
printf("%d\n",len/(len-nxt[len]));
else
puts("");
}
return ;
}//Cap ou pas cap. Pas cap.

POJ 2752: Seek the Name, Seek the Fame

题意:

输出所有s的合法前缀,合法前缀的定义为前缀等于后缀...

分析:

不断nxt...

代码:

 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
using namespace std;
//眉眼如初,岁月如故 const int maxn=+; int len,tail,nxt[maxn],stk[maxn]; char str[maxn]; inline void getnxt(void){
nxt[]=nxt[]=;int k;
for(int i=;i<len;i++){
k=nxt[i];
while(k&&str[k+]!=str[i+])
k=nxt[k];
if(str[k+]==str[i+])
nxt[i+]=k+;
else
nxt[i+]=;
}
} signed main(void){
while(scanf("%s",str+)!=EOF){
len=strlen(str+);getnxt();tail=;stk[++tail]=len;
while(nxt[len])
stk[++tail]=nxt[len],len=nxt[len];
for(int i=tail;i>=;i--)
printf("%d ",stk[i]);
puts("");
}
return ;
}//Cap ou pas cap. Pas cap.

POJ 3167: Cow Patterns

题意:

给出两个串,如果a和b匹配当且仅当ab中的每个对应位置的数字rank1和rank2相同,rank1代表当前元素前面小于他的元素个数,rank2代表当前元素前面等于他的元素个数,求出匹配串和模式串的匹配位置(开头位置)

分析:

KMP…裸的KMP…就是计算一下rank就好… 
因为数字不会超过25,所以直接暴力求rank就好…

代码:

 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
using namespace std;
const int maxn=+,maxk=+,maxs=+;
int n,k,s,a[][maxn],nxt[maxk],num[][maxn][maxs],vis[maxk];
inline int rank1(int len,int en,int id){
int st=en-len,ans=;
for(int i=;i<a[id][en];i++)
ans+=num[id][en][i]-num[id][st][i];
return ans;
}
inline int rank2(int len,int en,int id){
return num[id][en][a[id][en]]-num[id][en-len][a[id][en]];
}
inline void getnext(void){
nxt[]=nxt[]=;int p;
for(int i=;i<k;i++){
p=nxt[i];int a=rank1(p+,p+,),b=rank1(p+,i+,),c=rank2(p+,p+,),d=rank2(p+,i+,);
while(p&&(a!=b||c!=d))
p=nxt[p],a=rank1(p+,p+,),b=rank1(p+,i+,),c=rank2(p+,p+,),d=rank2(p+,i+,);
if(rank1(p+,p+,)==rank1(p+,i+,)&&rank2(p+,p+,)==rank2(p+,i+,))
nxt[i+]=p+;
else
nxt[i+]=;
}
}
inline void kmp(void){
int posa=,posb=,ans=,stk[maxn];
while(posa<=n){
if(rank1(posb,posa,)==rank1(posb,posb,)&&rank2(posb,posa,)==rank2(posb,posb,))
posa++,posb++;
else if(posb==)
posa++;
else
posb=nxt[posb-]+;
if(posb==k+)
stk[++ans]=posa-k,posb=nxt[posb-]+;
}
cout<<ans<<endl;
for(int i=;i<=ans;i++)
cout<<stk[i]<<endl;
}
signed main(void){
scanf("%d%d%d",&n,&k,&s);memset(num,,sizeof(num));
for(int i=;i<=n;i++)
scanf("%d",&a[][i]),num[][i][a[][i]]=;
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
for(int j=;j<=s;j++)
num[][i][j]+=num[][i-][j];
for(int i=;i<=k;i++)
scanf("%d",&a[][i]),num[][i][a[][i]]=;
for(int i=;i<=k;i++)
for(int j=;j<=s;j++)
num[][i][j]+=num[][i-][j];
getnext();kmp();
return ;
}

By NeighThorn

KMP--君住长江头,我住长江尾,日日思君不见君,共饮长江水的更多相关文章

  1. 【POJ3461】【KMP】Oulipo

    Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...

  2. gj11 多线程、多进程和线程池编程

    11.1 python中的GIL # coding=utf-8 # gil global interpreter lock (cpython) # python中一个线程对应于c语言中的一个线程 # ...

  3. HTML5文本

    1.重要文本.斜体文本 粗体:<strong></strong> 粗体:<b></b> 斜体:<em></em> 斜体:< ...

  4. 多线程复习 Rlock ,Condition,Semaphore

    #对于io操作来说,多线程和多进程性能差别不大 #1.通过Thread类实例化 import time import threading def get_detail_html(url): print ...

  5. NO.006-2018.02.11《卜算子·我住长江头》宋代:李之仪

    卜算子·我住长江头_古诗文网(bǔ) 卜算子·我住长江头 宋代:李之仪 我住长江头,君住长江尾.日日思君不见君,共饮长江水. 我居住在长江上游,你居住在长江下游. 天天想念你却见不到你,共同喝着长江的 ...

  6. 界面实例--旋转的3d立方体

    好吧,这里直接编辑源码并不允许设置css和js……毕竟会有危险……那直接放代码吧 <!DOCTYPE html> <html> <head> <meta ch ...

  7. python Condition

    import threading # 必须要使用condition的例子 # class XiaoAi(threading.Thread):# def __init__(self, lock):# s ...

  8. 第十章:Python高级编程-多线程、多进程和线程池编程

    第十章:Python高级编程-多线程.多进程和线程池编程 Python3高级核心技术97讲 笔记 目录 第十章:Python高级编程-多线程.多进程和线程池编程 10.1 Python中的GIL 10 ...

  9. IO和socket编程

    五一假期结束了,突然想到3周前去上班的路上看到槐花开的正好.放假也没能采些做槐花糕,到下周肯定就老了.一年就开一次的东西,比如牡丹,花期也就一周.而花开之时,玫瑰和月季无法与之相比.明日黄花蝶也愁.想 ...

随机推荐

  1. Linux 命令大全提供 500 多个 Linux 命令搜索

    Linux Command 在这里维持一个持续更新的地方 516 个 Linux 命令大全,内容包含 Linux 命令手册.详解.学习,值得收藏的 Linux 命令速查手册.请原谅我写了个爬虫,爬了他 ...

  2. 常见react面试题汇总

    已经开源 地址:https://github.com/nanhupatar...关注我们团队: React 中 keys 的作用是什么? Keys 是 React 用于追踪哪些列表中元素被修改.被添加 ...

  3. Swoole 4.1.0 正式版发布,支持原生 Redis/PDO/MySQLi 协程化

    重大新特性 支持 Redis/PDO/MySQLi 从4.1.0版本开始支持了对PHP原生Redis.PDO.MySQLi协程化的支持. 可使用Swoole\Runtime::enableCorotu ...

  4. 24.VUE学习之-变异方法filter与regexp实现评论搜索功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. The 2016 ACM-ICPC Asia Shenyang Regional Contest

    A. Thickest Burger 大数 × 2 + 小数 #include <cstdio> #include <algorithm> using namespace st ...

  6. Codeforces Round #464 (Div. 2) B. Hamster Farm

    B. Hamster Farm time limit per test2 seconds memory limit per test256 megabytes Problem Description ...

  7. Linux系统软件包之---Apache

    当前互联网主流web服务说明 静态服务: apache 中小型静态web服务的主流,web服务器中的老大哥 nginx 大型新兴网站静态web服务主流,web服务器中的初生牛犊 lighttpd 静态 ...

  8. 处理IE6下PNG图片透明背景问题

    由于历史原因,IE较早的版本不支持PNG透明 可以支持GIF等的透明 由于png图片相对较小,所以很多网站还是青睐于PNG图片 最近就遇到这种情况,使用js和css滤镜来实现的与大家分享一下下: 首先 ...

  9. cf975d Ghosts

    ref #include <algorithm> #include <iostream> #include <cstdio> #include <map> ...

  10. centos使用--zsh

    目录 1 切换到zsh 1.1 查看系统当前的shell 1.2 查看bin下是否有zsh包 1.3 安装zsh包 1.4 切换shell至zsh 2 安装oh-my-zsh 2.1 oh-my-zs ...