hdu 2594 Simpsons’ Hidden Talents(数据结构:KMP)
来源:互联网 发布:阿里云服务器建站助手 编辑:程序博客网 时间:2024/05/16 17:32
这道题的数据太弱了,弱到我不敢相信。。。
我的做法是把s1 和 s2连接起来求next数组
再利用next数组性质来找next[len1+len2]对应的前缀
用这种方法有个问题就是如果求出的前缀超出了s1或s2的长度怎么办?
看到有一个题解的方法是输出len1 len2中较小值对应的数组长度。。。这错的简直离谱
先把错误代码贴在这里:
#include<stdio.h>#include<string.h>#define MAX_STR 50005char str1[MAX_STR*2],str2[MAX_STR]; int next[MAX_STR*2];void get_next(int,int);int main(){ char oupt[MAX_STR]; while(scanf("%s%s",str1,str2)!=EOF) { int len1=strlen(str1),len2=strlen(str2); strcat(str1,str2); get_next(len1,len2); int i; for(i=0;i<next[len1+len2];i++) { oupt[i]=str1[i]; } oupt[i]='\0'; if(next[len1+len2]) { if(next[len1+len2]>len1||next[len1+len2]>len2) { if(len1>len2) { printf("%s %d\n",str2,len2); } else { oupt[len1]='\0'; printf("%s %d\n",oupt,len1); } } else { printf("%s %d\n",oupt,next[len1+len2]); } } else { printf("0\n"); } } return 0;}void get_next(int len1,int len2){ int i=0,j=-1; next[0]=-1; while(i<len1+len2) { if(j==-1||str1[i]==str1[j]) { i++;j++; next[i]=j; } else { j=next[j]; } }}
用这个代码跑样例:
ababa
baba
结果是baba 4
但是很明显答案是aba 3!!!
用这份代码交的居然还跑出来了0ms AC
这尼玛。。。
我的做法是如果求出的长度超出了s1 或 s2的长度
就从其中较小的长度开始从大到小枚举对应长度的前缀和后缀比较,如果相等则输出对应结果并退出
这样做的时间复杂度是O(strlen(str)),所以不会超时
用C++提交WA,用G++提交15ms AC
但是想到上面那份逗比代码0msAC,我就各种不爽。。。
我的代码如下:
//用G++提交!!!#include <cstdio>#include <cstring>#define MAXN 100100using namespace std;int next[MAXN];char chs1[MAXN>>1], chs2[MAXN>>1];char str1[MAXN>>1], str2[MAXN>>1];void get_next(char p[]) { int len = strlen(p); int i = 0, j = -1; next[0] = -1; while(i < len) { if(j==-1 || p[i]==p[j]) { ++i, ++j; next[i] = j; } else j = next[j]; }}int main(void) { int len1, len2; while(scanf("%s%s", str1, str2) != EOF) { len1 = strlen(str1); len2 = strlen(str2); strcat(str1, str2); //printf("%s\n", str1); get_next(str1);/* for(int i=0; i<=len1+len2; ++i) printf("%d\t", next[i]); puts("");*/ int tmp = next[len1+len2]; if(tmp>0 && tmp<=len1 && tmp<=len2) { strcpy(chs1, str1+len1+len2-tmp); printf("%s %d\n", chs1, tmp); } else if(tmp == 0) { printf("0\n"); } else { tmp = len1 > len2 ? len2 : len1; int i; for(i=tmp; i>0; --i) { memset(chs1, 0, sizeof(chs1)); memset(chs2, 0, sizeof(chs2)); strncpy(chs1, str1, i); //printf("chs1 = %s\n", chs1); strcpy(chs2, str1+len1+len2-i); //printf("chs2 = %s\n", chs2); if(strcmp(chs1, chs2) == 0) { printf("%s %d\n", chs1, i); break; } } if(i == 0) printf("0\n"); } memset(str1, 0, sizeof(str1)); memset(str2, 0, sizeof(str2)); memset(next, 0, sizeof(next)); } return 0;}
0 0
- hdu 2594 Simpsons’ Hidden Talents(数据结构:KMP)
- HDU 2594 Simpsons’ Hidden Talents(KMP)
- KMP hdu-2594 Simpsons’ Hidden Talents
- HDU 2594 Simpsons’ Hidden Talents KMP
- HDU 2594Simpsons’ Hidden Talents(KMP运用)
- HDU 2594 Simpsons’ Hidden Talents KMP
- hdu 2594 Simpsons’ Hidden Talents(KMP)
- hdu 2594 Simpsons’ Hidden Talents(KMP)
- HDU 2594 Simpsons’ Hidden Talents(KMP)
- hdu 2594 Simpsons’ Hidden Talents(KMP)
- HDU 2594 Simpsons’ Hidden Talents KMP
- HDU 2594 Simpsons’ Hidden Talents (KMP)
- hdu 2594 Simpsons’ Hidden Talents KMP
- HDU 2594 Simpsons’ Hidden Talents KMP题解
- HDU 2594 Simpsons’ Hidden Talents(KMP)
- hdu 2594 Simpsons’ Hidden Talents (kmp扩展)
- 【KMP】 HDU 2594 Simpsons’ Hidden Talents
- hdu 2594 Simpsons’ Hidden Talents ( kmp )
- NSDate 和NSDateFormate
- OC的ARC机制
- libsvm分布式并行调参
- hdu 2063
- 我可以忘记你
- hdu 2594 Simpsons’ Hidden Talents(数据结构:KMP)
- poj 1469
- 类似于支付宝手势页面 UILabel抖动效果
- 利用Servlet导出Excel
- 资深站长对云主机的评价
- 敏捷开发之产品级经验分享
- Sum It Up
- hdu 1068
- 栈的表示和实现