hdu1711 hdu1686 hdu3336 hdu1358
来源:互联网 发布:软件游戏猎手 编辑:程序博客网 时间:2024/06/06 04:54
hdu 1711
kmp算法模板题,没明白问什么这里下标一定要是1开始,0开始就错了,或许是我还有地方没想到只是过了样例而已,改为1过了
代码:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<cmath>#include<map>#include<algorithm>using namespace std;const int LEN1=1000105;const int LEN2=10105;int s[LEN1], t[LEN2], next[LEN2]; void get_next(const int&m){ int i=1, j=0; next[1]=0; while( i<=m ) { if( 0==j || t[i]==t[j] ) { ++i; ++j; next[i]=j; } else j=next[j]; }}int func(const int&n, const int&m){ int i=1, j=1; while( i<=n && j<=m ) { if( 0==j || s[i]==t[j] ) { ++i; ++j; } else j=next[j]; } if( j>m ) return i-m; return -1;}int main(int argc, char *argv[]){ int T, n, m; cin>>T; while( T-- ) { scanf("%d%d", &n, &m); for(int i=1; i<=n; i++) scanf("%d", s+i); for(int i=1; i<=m; i++) scanf("%d", t+i); get_next(m); printf("%d\n", func(n, m)); } return 0;}
hdu 1686
和hdu 1711差不多,不过这里是求s串里最大有几个t串
代码:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<cmath>#include<map>#include<algorithm>using namespace std;const int LEN1=1000105;const int LEN2=10105;char s[LEN1], t[LEN2];int next[LEN2]; void get_next(){int i=0, j=-1, m=strlen(t); next[0]=-1;while( i<m ){if( -1==j || t[i]==t[j] ){++i; ++j; next[i]=j;}else j=next[j];}}int kmp(){int i=0, j=0, n=strlen(s), m=strlen(t);int times=0;while( i<n ){if( -1==j || s[i]==t[j] ){++i; ++j;}else j=next[j];/*这里不是j=0;因为i到这里 s串的 i-next[j]+1 ~~~ i-1会和 t串的 0 ~~~ next[j]-1相同但不一定s[i]和t[0]相同,所以如果 j=0会减少最大子串的数目; */ if( j>=m ) times++, j=next[j];}return times;}int main(int argc, char *argv[]){int T;cin>>T;while( T-- ){scanf("%s", t);scanf("%s", s);get_next();printf("%d\n", kmp());}return 0;}
hdu1358:
思路:因为此题是一个子字符串循环得到的母串,所以没有特判;直接就是算后面的是前面的小字符串的多少倍,利用next数组,因为
next数组存放的是与前缀相同的最长的序列下标到了哪里;所以 i - next[i] 表示next[i]+1 ~ i 都是1 ~ next[i] 的字符串复制得到;所以做就行了
代码:
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<cmath>#include<map>#include<algorithm>using namespace std;const int LEN=1000005;const int INF=0x3f3f3f3f;const int MOD=10007;int next[LEN]; char s[LEN], t[LEN];int cns[LEN];int main(int argc, char *argv[]){int i, j, lt, cas=0;while( scanf("%d", <)!=EOF && lt ){getchar();scanf("%s", t+1);i=1; j=0; next[1]=0;while( i<=lt ){if( 0==j || t[i]==t[j] ){++i; ++j;next[i]=j;}else j=next[j];}for(i=1; i<=lt; i++) cout<<next[i]<<" "; cout<<endl;printf("Test case #%d\n", ++cas);for(i=3, lt++; i<=lt; i++){j=(i-1)/(i-next[i]);if( j>1 && (i-1)%(i-next[i])==0 )printf("%d %d\n", i-1, j);}printf("\n");}return 0;}
hdu3336
思路:
代码:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<cmath>#include<map>#include<algorithm>using namespace std;const int LEN = 1000005;const int MOD=10007;char t[LEN];int next[LEN];int main(int argc, char *argv[]){ int n, i, j, T; cin>>T; while( T-- ) { scanf("%d%s", &n, t); i=0; j=-1; next[0]=-1; while( i<n ) { if( -1==j || t[i]==t[j] ) next[++i]=++j; else j=next[j]; } //for(i=0; i<n; i++) cout<<next[i]<<" "; //cout<<endl; int sum=0, pos; for(i=1; i<=n; i++) { pos=i; while( pos ) { sum=(sum+1)%MOD; pos=next[pos]; } } printf("%d\n", sum); } return 0;}
0 0
- hdu1711 hdu1686 hdu3336 hdu1358
- hdu1686
- hdu1686
- hdu1686
- hdu1686
- hdu1686
- HDU1686
- HDU1686
- hdu1358
- hdu1358
- hdu1358
- hdu1358
- hdu1358
- hdu3336
- hdu3336
- hdu3336
- hdu3336
- hdu1711
- Android Loader详解四:回调及完整例子
- IOS中NSUserDefaults的用法
- 在VC和VS中使用条件断点
- UVA 1414 - Hanoi Towers(dp)
- 共享池之九:绑定变量与session_cached_cursors
- hdu1711 hdu1686 hdu3336 hdu1358
- hdu 2087 剪花布条(KMP)
- 多重背包问题的单调队列优化 转载
- 新建的Android项目下src和layout下面没有任何东西
- hdu2066 一个人的旅行
- 给定两个正整数,求出两数的正差值并输出
- OpenGL帧缓冲区 转载
- hdu2066
- 国外程序员推荐:每个程序员都应读的书