hdu 1711为何WA这么多次
2015-02-02 16:04
169 查看
WA的代码:
AC的代码
#include <iostream> #include<stdio.h> #include<string> #include<string.h> const int maxnum=10010; using namespace std; int mother[1000010]; int son[maxnum]; int next[maxnum]; int chang1,chang2; void build() { int i,j=0; next[1]=0; for(i=2;i<=chang2;i++) { while(j>0&&son[j+1]!=son[i]) j=next[j]; if(son[j+1]==son[i]) j++; next[i]=j; } } int kmp() { int i=1,j=1; while(i<=chang1&&j<=chang2) { if(mother[i]==son[j]||j==0) { i++; j++; if(j==chang2+1) return i-j+1; } else j=next[j]; } return -1; } int main() { int casenumber; scanf("%d",&casenumber); while(casenumber--) { scanf("%d%d",&chang1,&chang2); int i; for(i=1;i<=chang1;i++) scanf("%d",&mother[i]); for(i=1;i<=chang2;i++) scanf("%d",&son[i]); if(chang2>chang1) printf("-1\n"); else { memset(next,0,sizeof(next)); build(); for(i=1;i<=chang2;i++) cout<<next[i]<<" "; int ans=kmp(); printf("%d\n",ans); } } return 0; }
AC的代码
#include <iostream> #include<stdio.h> #include<string> #include<string.h> const int maxnum=10010; using namespace std; int mother[1000010]; int son[maxnum]; int next[maxnum]; int chang1,chang2; void build() { int i,j=0; next[1]=0; i=1;j=0; while(i<chang2) { if(j==0||son[i]==son[j]) { i++; j++; if(son[i]!=son[j]) next[i]=j; else next[i]=next[j]; } else j=next[j]; } } int kmp() { int i=1,j=1; while(i<=chang1&&j<=chang2) { if(mother[i]==son[j]||j==0) { i++; j++; if(j==chang2+1) return i-j+1; } else j=next[j]; } return -1; } int main() { int casenumber; scanf("%d",&casenumber); while(casenumber--) { scanf("%d%d",&chang1,&chang2); int i; for(i=1;i<=chang1;i++) scanf("%d",&mother[i]); for(i=1;i<=chang2;i++) scanf("%d",&son[i]); if(chang2>chang1) printf("-1\n"); else { memset(next,0,sizeof(next)); build(); //for(i=1;i<=chang2;i++) // cout<<next[i]<<" "; int ans=kmp(); printf("%d\n",ans); } } return 0; }问题主要是出在求next值上,想不通到底是为什么会造成这些差异
相关文章推荐
- 三维计算几何模板 hdu 5733 tetrahedron(不知为何WA)
- HDU 1711 Number Sequence(KMP算法)
- HDU-1711
- 为何毕业三年后的差距这么大?
- HDU - 1711(Number Sequence )(KMP)
- HDU 1711 Number Sequence(KMP模板)
- hdu 1711 Number Sequence(kmp模板题)
- HDU 1711 Number Sequence
- HDU 1711 Number Sequence
- HDU-#1711 Number Sequence(KMP)
- HDU 1711 Number Sequence
- HDU 1711—— Number Sequence(KMP 入门)
- AC日记——Number Sequence hdu 1711
- HDU 1711 KMP
- hdu 2604(WA)
- hdu 1711 Number Sequence
- hdu 1711 Number Sequence(KMP)
- [KMP模板题]HDU-1711 Number Sequence
- 奇葩的uva102(多次WA后已AC)
- hdu 1711 KMP