poj 3080 Blue Jeans【字符串】
大致题意:
就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10
规定:
1、 最长公共串长度小于3不输出
2、 若出现等长的最长的子串,则输出字典序最小的串
解体思路: 暴力。
View Code
#include<stdio.h> #include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; vector<string> vec; int main() { int n; while(scanf("%d", &n) != EOF) { while( n-- ) { vec.clear(); int m; scanf("%d", &m); for(int i = 0; i < m; i++) { string temp; cin >> temp; vec.push_back(temp); } int cnt = 60; string ans; for(int len = 1; len <= 60; len++) { //从长度为1开始循环,一直到长度为60。 for(int i = 0; i < cnt; i++) { //当长度为1时,串分成60份,循环60次。 //以vec[0]作为母串,i为母串的起始位置,len为母串的长度,构造匹配串str。 string str; str = vec[0].substr(i, len);//生成子串函数,i为起始位置,len为子串长度。 bool Find = 1; //默认为1,说明都找到了。 for(int j = 1; j < m; j++) { int pos = vec[j].find(str); if(pos == string::npos) { Find = 0; // 没找着。 break; } } if( Find ) { if(ans.length() == str.length()) { ans = min(ans, str); } else { ans = str; } } } cnt--; //每次大的循环cnt都要减1,因为长度为1的时候分成60份,长度为2的时候分成59份... } if(ans.length() >= 3) { cout << ans << endl; } else { cout << "no significant commonalities\n"; } } } return 0; }