m 條 DNA 序列(m ≤ 10,長 ≤ 60),找最長公共子串(至少長度 1)。輸出字典序最小者,無則輸出 "no significant commonalities"。
str.substr(i,L) 比較,C++ set<string> 自動去重排序#include <cstdio>
#include <set>
#include <string>
#include <algorithm>
using namespace std;
char s[11][65];
int main() {
int T; scanf("%d",&T);
while(T--) {
int m; scanf("%d",&m);
for(int i=0;i=1; --L) { // 從長到短枚舉
set common;
for(int i=0; i+L<=strlen(s[0]); ++i)
common.insert(string(s[0]+i, L));
for(int k=1; k cur;
for(int i=0; i+L<=strlen(s[k]); ++i) {
string sub(s[k]+i, L);
if(common.count(sub)) cur.insert(sub);
}
common.swap(cur);
if(common.empty()) break;
}
if(!common.empty()) { ans = *common.begin(); break; }
}
if(ans.empty()) puts("no significant commonalities");
else printf("%s\n", ans.c_str());
}
return 0;
}