POJ2752 Seek the Name, Seek the Fame
来源:互联网 发布:手游试玩赚钱软件 编辑:程序博客网 时间:2024/05/18 13:45
原题:
Seek the Name, Seek the Fame
第一组样例解释:
样例一:ababcababababcabab 2:ababcababababcabab 4:ababcabababcabab 9:ababcabab ababcabab 18: 全部
样例二:aaaaa 1 2 3 4 5 不再一一解释;
一开始我想的是直接运用string的substr函数代码如下:
while(cin>>str){for(int i=0;i<str.length();i++){s1=str.substr(0,i+1);s2=str.substr(str.length()-i-1,str.length());if(s1==s2)cout<<s1.length()<<" ";}cout<<endl;}
答案是肯定的:Time Limit Exceeded 所以要找一种时间复杂度更短的算法,我不想再铤而走险,
暴力救不了中国人,只能学习新的知识,KMP算法:
KMP算法的核心思想是利用已经得到的部分匹配信息来进行后面的匹配过程。
这里有一个很好的关于KMP的转载:http://blog.csdn.net/lin_bei/article/details/1252686
附上神奇的代码,KMP:
#include<cstdio>#include<string>#include<string.h>#include<iostream>using namespace std;int next[400005],a[400005];string str;int v[400005];void Get_next(){int t = -1,j = 0;next[0] = -1;while(j < str.length()){if(t == -1||str[j] == str[t]){j++; t++;next[j] = t;}elset = next[t];}}int main(){int t;while(cin>>str){t=0;memset(v,0,sizeof(v));Get_next();int j = next[str.length()];while(j > 0)a[t++] = j , j = next[j];for(int i = t-1; i >= 0; i--)cout<<a[i]<<" ";cout<<str.length()<<endl;}return 0;}
阅读全文
0 0
- POJ2752 Seek the Name, Seek the Fame
- POJ2752:Seek the Name, Seek the Fame
- poj2752 Seek the Name, Seek the Fame
- POJ2752 Seek the Name, Seek the Fame
- poj2752 seek the name seek the fame
- poj2752 Seek the Name, Seek the Fame
- POJ2752 Seek the Name, Seek the Fame
- poj2752-Seek the Name, Seek the Fame
- poj2752 Seek the Name,Seek the Fame
- POJ2752 Seek the Name, Seek the Fame
- poj2752 seek the name,seek the fame
- POJ2752 Seek the Name, Seek the Fame
- POJ2752 Seek the Name, Seek the Fame
- poj2752 Seek the Name, Seek the Fame
- POJ2752-Seek the Name, Seek the Fame
- [POJ2752] Seek the Name,Seek the Fame
- POJ2752 Seek the Name, Seek the Fame
- 【POJ2752】seek the name,seek the fame
- mac配置完ssh依然提示"Enter passphrase for key"解决方法
- 换新电脑了 下载工程发现需要密码 这是如何解决呀
- MFC接受参数的三种方法
- java中finally关键字
- CocoaPods 安装第三方时出现的问题及解决
- POJ2752 Seek the Name, Seek the Fame
- Xshell/Secure CRT/PuTTY使用密钥对登录阿里云Linux服务器
- HTMl和css的hack技术常识
- 判断链表是否带环?若带环求环的长度?求环的入口点?
- Tomcat下使用war包发布项目
- js对于字符串的一些简单操作
- css去掉a标签点击后的虚线框
- ListView的adapter中设置item宽高
- MySQL学习之——锁(行锁、表锁、页锁、乐观锁、悲观锁等)