1. 程式人生 > 實用技巧 >題解— blue jeans (KMP演算法)

題解— blue jeans (KMP演算法)

題目連結:https://cn.vjudge.net/contest/388654#problem/A

字串匹配kmp模板題

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <string>
#include <cmath>
#include <vector>
#include <queue>

using namespace std;
typedef 
long long ll; string s[15], strmax; int main() { int n; cin >> n; while (n--) { strmax = ""; int m; cin >> m; for (int i = 0; i < m; i++) { cin >> s[i]; } for (int i = 0; i <= 60; i++) {
for (int j = 0; j <= 60; j++) { int flag = 0; string temp = s[0].substr(i, j); for (int k = 1; k < m; k++) { if (s[k].find(temp) == string::npos) { flag = 1;
break; } } if (!flag) { if (temp.size() > strmax.size()) { strmax = temp; } else if (temp.size() == strmax.size() && temp < strmax) { strmax = temp; } } } } if (strmax.size() < 3) cout << "no significant commonalities" << endl; else cout << strmax << endl; } return 0; }