kmp poj 3450 3461
来源:互联网 发布:readonly dos linux 编辑:程序博客网 时间:2024/05/01 21:43
这两题都比较简单,很明显的kmp求解
poj 3450
题目大意:就是求最长的公共子串,如果有长度一样的,取字典序最小的
跟poj 3080基本一模一样,没啥好说的~
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn = 4010;const int maxm = 210;int n, next[maxm];char str[maxn][maxm];void get_next(int start, int len);bool compare(int start, int len, int index);int main(){ while(true) { scanf("%d", &n); if(n == 0) break; bool inital = true; char ans[maxm] = "IDENTITY LOST"; for(int i = 0; i < n; i++) scanf("%s", str[i]); int len = strlen(str[0]); bool sign = false; for(int i = len; i >= 1; i--) { for(int j = 0; j + i <= len; j++) { get_next(j, i); bool flag = true; for(int k = 1; k < n; k++) { if(!compare(j, i, k)) { flag = false; break; } } if(flag) { if(inital || strncmp(ans, str[0]+j, i) > 0) { inital = false; strncpy(ans, str[0]+j, i); ans[i] = '\0'; } sign = true; } } if(sign) break; } printf("%s\n", ans); } return 0;}void get_next(int start, int len){ int pos = start - 1; int i = start, j = pos, end = start + len; next[start] = pos; while(i < end) { if(j == pos || str[0][i] == str[0][j]) { i++; j++; next[i] = j; } else j = next[j]; }}bool compare(int start, int len, int index){ int pos = start - 1; int i = start, j = 0, end = start + len; int length = strlen(str[index]); while(i < end && j < length) { if(i == pos || str[0][i] == str[index][j]) { i++; j++; } else i = next[i]; } if(i == end) return true; return false;}
poj 3461
这题也很简单,水题~
题目大意:给几组字符串数据,要你求匹配串在模式串里面出现了几次
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn = 1000010;const int maxm = 10010;int next[maxm], n;char str[maxn], p[maxm];int main(){ scanf("%d", &n); int i, j, plen, slen, ans; while(n-- != 0) { ans = 0; scanf("%s", p); scanf("%s", str); plen = strlen(p); slen = strlen(str); i = 0, j = -1; next[0] = -1; while(i < plen) { if(j == -1 || p[i] == p[j]) { i++; j++; next[i] = j; } else j = next[j]; } i = 0; j = 0; while(j < slen) { if(i == -1 || p[i] == str[j]) { i++; j++; } else i = next[i]; if(i == plen) { ans++; i = next[i]; } } printf("%d\n", ans); } return 0;}
- kmp poj 3450 3461
- POJ 3080 POJ 3450 KMP
- POJ-3450 3080 KMP
- 【KMP模板】POJ 3461
- poj 3461 KMP
- poj 3461 Oulipo(KMP)
- POJ 3461 KMP
- POJ 3461 kmp
- POJ 3461 Oulipo(KMP)
- poj 3461 kmp
- POJ 3461 Oulipo---kmp
- POJ 3461 Oulipo KMP
- poj 3461 Oulipo (KMP)
- POJ 3461 Oulipo ( KMP )
- POJ 3461 裸KMP
- 【POJ 3461 KMP】
- KMP poj 3461
- kmp poj 3461
- 419
- zen-cart五大必用插件,模块和工具
- Android NDK使用的小问题
- 检查IPv4和IPv6地址的正则表达式
- BGA焊盘设计的一般规则
- kmp poj 3450 3461
- 月薪800终于买新车~爽~500公里感受
- PHP中的乱码问题
- [Arch iwlist issue] Allocation failed
- Spring3, Hibernate3.6与Proxool连接池配置
- VC++内联汇编(MSDN相关内容完整翻译)
- 试题: 小白鼠问题
- SQL Server 2012提供的OFFSET/FETCH NEXT与Row_Number()对比测试
- SQL SERVER 2012 T-SQL新增聚合函数