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. node第一天

    一.主要执行的文件命名一般为main.js var aModule =require('./a.js');//相对路径 var aModule =require('a.js');//专门从node_m ...

  2. Mysql忘记密码找回步骤

    Mysql密码忘记找回步骤: 1.首先停止数据库 [root@localhost ~]# /etc/init.d/mysqld stop 2.使用--skip-grant-tables启动mysql, ...

  3. [BZOJ1503]郁闷的出纳员(Splay)

    Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常 ...

  4. 4 Template层 -定义模板

    1.模板介绍 作为Web框架,Django提供了模板,可以很便利的动态生成HTML 模版系统致力于表达外观,而不是程序逻辑 模板的设计实现了业务逻辑(view)与显示内容(template)的分离,一 ...

  5. MySQL之索引(一)

    创建高性能索引 索引是存储引擎用于快速找到记录的一种数据结构.这是索引的基本功能.索引对于良好的性能非常关键.尤其是当表中的数据量越来越大时,索引对性能的影响愈发重要.在数据量较小且负载较低时,不恰当 ...

  6. Spring Boot 学习系列(03)—jar or war,做出你的选择

    此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 两种打包方式 采用Spring Boot框架来构建项目,我们对项目的打包有两种方式可供选择,一种仍保持原有的 ...

  7. 46、android studio第一次使用时卡在gradle下载怎么解决?

    如果没法FQ或者FQ后网速慢,哥教你一个快速解决方案. 在根目录下的.gradle目录下,找到wrapper/dists目录,如果当前正在下载gradle.x.xx-all.zip,那么会发现grad ...

  8. 【NOIP 2012】借教室

    题目 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借教室的信息,我们自然希望 ...

  9. log4j2用Log4jContextSelector启动参数配置全局异步日志是如何使用disruptor

    与 log4j2用asyncRoot配置异步日志是如何使用disruptor差异有几个: 给disruptor实例的EventFactory不同 此处EventFactory采用的是RingBuffe ...

  10. Jmeter随笔一

    资料分享:http://www.cnblogs.com/yangxia-test/p/3964881.html