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. C++ 无限定名称查找

    无限定名称查找 (关键字:懒惰,挑捡,using指令的特殊性) 无限定名称查找实际上就是指没有限定(名称空间和名称空间运算符)名存在的一个名字的出现,其中对于using指令,其内部包含的所有的声明是被 ...

  2. centos 7 安装以及ip配置

    1.安装: root 200M: swap 内存的2倍,如果内存超过4g,最大设为8g就够了: / 剩余: 2.ip配置 (1)动态配置:dhclient ip add (2)静态配置:vi /etc ...

  3. springboot中加入druid对sql进行监控

    springboot作为现在十分流行的框架,简化Spring应用的初始搭建以及开发过程,现在我们就使用springboot来进行简单的web项目搭建并对项目sql进行监控. 项目的搭建就省略了,spr ...

  4. 项目10.2-企业级自动化运维工具---puppet详解

    1.认识puppet 1.1 引入 puppet是什么,咱们先不用专业的名词解释它,咱们先描述一些工作场景,看明白这些工作场景,自然会知道puppet是什么. (1)场景一: 管理员想要在100台服务 ...

  5. 二、Shell 变量

    Shell 变量 定义变量时,变量名不加美元符号($,PHP语言中变量需要),如: your_name="runoob.com" 注意,变量名和等号之间不能有空格,这可能和你熟悉的 ...

  6. day09-函数讲解

    1.如何定义一个函数 s = '华为加油a' def s_len(): i = 0 for k in s: i += 1 print(i) s_len() 这个函数的功能就是输出字符串的长度.但是他只 ...

  7. Network of Schools POJ - 1236 (强联通)

    一些学校连接到了一个计算机网络.网络中的学校间有如下约定:每个学校维护一个列表,当该学校收到软件或信息后将会转发给列表中的所有学校(也就是接收方列表).需要注意的是如果B学校在A学校的接收方列表中,A ...

  8. Diycode开源项目 搭建可以具有下拉刷新和上拉加载的Fragment

    1.效果预览 1.1.这个首页就是一个Fragment碎片,本文讲述的就是这个碎片的搭建方式. 下拉会有一个旋转的刷新圈,上拉会刷新数据. 1.2.整体结构 首先底层的是BaseFragment 然后 ...

  9. [项目2] 10mins

    1.准备工作 M层:生成虚假数据 from django.db import models from faker import Factory # Create your models here. c ...

  10. hdu1950Bridging signals(求最长上升自序列nlogn算法)

    Bridging signals Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...