poj3080 kmp
来源:互联网 发布:奥尔加隆 知乎 编辑:程序博客网 时间:2024/05/21 22:45
1.直接用string
#include <stdio.h>#include <string.h>#include <iostream>using namespace std;string str[15];int main(){ ios::sync_with_stdio(0); int T, i, j, n, k; cin >> T; string tmp, sub; while (T--) { bool flag = false; cin >> n; for (i = 0; i < n; i++) { cin >> tmp; str[i] = tmp; } string ans; for (i = 0; i < str[0].size(); i++) for (j = 3; j + i - 1 < str[0].size(); j++) { sub = str[0].substr(i, j); for (k = 1; k < n; k++) if (str[k].find(sub) == string::npos) break; if (k == n) { flag = true; if (ans.size() < sub.size()) ans = sub; else if (ans.size() == sub.size()) ans = min(ans, sub); } } if (flag) cout << ans << endl; else cout << "no significant commonalities" << endl; } return 0;}
2.kmp做法
#include <stdio.h>#include <string.h>#include <iostream>using namespace std;int cnt, f[70], flag;char p[70];void getfail(){ f[0] = f[1] = 0; for(int i = 1; i < strlen(p); i++) { int j = f[i]; while(j && p[i] != p[j]) { j = f[j]; } f[i + 1] = p[i] == p[j] ? j + 1 : 0; }}bool finnd(char *s1){ int j = 0; int len, len1; len = strlen(p); len1 = strlen(s1); for(int i = 0; i < len1; i++) { while(j && p[j] != s1[i]) { j = f[j]; } if(p[j] == s1[i]) { j++; } if(j == len) { return true; } } return false;}int main(){ int t, n; std::ios::sync_with_stdio(false); scanf("%d", &t); while(t--) { flag = 0; cnt = 0; scanf("%d", &n); char s[15][70], ans[70]; for(int i = 0; i < n; i++) { scanf("%s", s[i]); } int len; len = strlen(s[0]); //printf("%d\n", len); int j, q, r, l; memset(ans, '\0', sizeof(ans)); int maxlen = 0; for(j = 0; j < len; j++) { l = 0; memset(p, '\0', sizeof(p)); for(q = j; q < len; q++) { p[l++] = s[0][q]; // l++; cnt = 0; memset(f, 0, sizeof(f)); getfail(); // printf("%s\n", p); for(r = 1; r < n; r++) { //printf("-1\n"); if(finnd(s[r])) { cnt++; } } // printf("-1\n"); if(cnt == n - 1) { flag = 1; if(maxlen < l) { //strcpy(ans, p); maxlen = l; memcpy(ans, p, sizeof(p)); } else if(maxlen == l) { if(strcmp(ans, p) > 0) // strcpy(ans, p); memcpy(ans, p, sizeof(p)); } } } } if(flag && maxlen >= 3) { cout<<ans<<endl; } else cout<<"no significant commonalities"<<endl; }}
0 0
- poj3080(kmp)
- poj3080 kmp
- poj3080 kmp+枚举
- poj3080 Blue Jeans-------KMP
- poj3080之KMP入门
- poj3080~KMP+枚举
- poj3080 枚举+ kmp
- poj3080(枚举+kmp)
- POJ3080 Blue Jeans(kmp & 暴力)
- POJ3080 Blue Jeans(暴力kmp)
- POJ3080 Blue Jeans 【KMP 暴力水过】
- POJ3080-Blue Jeans(KMP,水)
- POJ3080
- poj3080
- poj3080
- poj3080
- poj3080
- poj3080
- 第六周项目1-分数类的雏形
- css入门
- PHP之创建图像验证码 ---实例
- Android动画原理分析
- ejs 请求路径问题
- poj3080 kmp
- MyBatis的一些资料
- 错误10error LNK1104: 无法打开文件“kernel32.lib”
- poj 2115 C Looooops
- Merge Two Sorted Lists(和并两个从小到大排好序的链表)
- 高质量 Android 开发框架 LoonAndroid 详解
- CUDA 7.5 & cuDNN v4 & tensorflow on Ubuntu 14.04 LTS
- iOS Project bootstrap
- java包package之二