poj 2752 (summerIII seek the name,seek the fame)
来源:互联网 发布:使命召唤11优化怎么样 编辑:程序博客网 时间:2024/06/11 16:43
这道题真的加深了我对next数组的理解,。没想除了用在KMP上还能有这种操作。。。开始想着用其中的K的值做,因为根据定义,k代表的是最长相同的前后缀长度。。。然后不知道为什么WA。。。。后来百度了一下别人的做法恍然大悟,觉得精妙无比。就是通过递归next数组然后求得前后缀的长度。
以下为链接点击打开链接
AC代码:
#include<cstdio>#include<cstring>#include<string.h>#include<iostream>using namespace std;const int maxn=400000+50;char str[maxn];int sum[maxn],next[maxn],num;void makeNext(const char P[],int next[]){ int q,k; int m=strlen(P);next[0]=0; for(q=1,k=0;q<m;++q) { while(k>0&&P[q]!=P[k]) k=next[k-1]; if(P[q]==P[k]) k++; next[q]=k; } for(int i=m;i>0;) { num++; //cout<<"next:"<<next[i-1]<<endl; sum[num]=next[i-1];///每次都忘记这里要减一,字符串最后一位为‘\0’; i=next[i-1]; }}int main(){ while(scanf("%s",str)!=EOF) { int len=strlen(str); num=0; memset(next,0,sizeof(next)); memset(sum,0,sizeof(sum)); makeNext(str,next); for(int i=num-1;i>=1;i--) printf("%d ",sum[i]); printf("%d\n",len); } return 0;}
阅读全文
0 0
- poj 2752 (summerIII seek the name,seek the fame)
- poj seek the name,seek the fame
- poj---Seek the Name, Seek the Fame
- Seek the Name, Seek the Fame POJ
- Seek the Name, Seek the Fame POJ
- POJ-2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame(KMP)
- POJ 2752--Seek the Name, Seek the Fame (next)
- POJ:2752Seek the Name, Seek the Fame
- POJ 2752 - Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- POJ-2752-Seek the Name, Seek the Fame
- POJ 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- POJ 2752 Seek the Name, Seek the Fame
- yum安装openjdk出现No more mirrors to try问题解决
- IDEA配置SVN(一)
- 关于写java web项目一些问题
- 2017 Summer Training Day1
- rocketmq问题汇总-如何将特定消息发送至特定queue,消费者从特定queue消费
- poj 2752 (summerIII seek the name,seek the fame)
- Angular4+NodeJs+MySQL 入门-05 接口调用
- 1007. 素数对猜想 (20)
- activemq 消息选择器Selector
- HDFS是什么?HDFS适合做什么?我们应该怎样操作HDFS系统?
- 【自学PHP】第二课:PHP数据类型
- flask笔记
- 字符串处理(4)——字符串常用操作(字符串链接,比较,截取,查找,大小转换,内容替换,获取某位置的字符)
- Web前端 PS切图