题目链接:http://codeforces.com/contest/861/problem/D
解题思路:
优雅的暴力。
对于输入的每一个号码,从短到长找出它的所有子串,用 vector 保存每个号码对应的所有子串,用 map 为所有子串做标记,输出答案的时候只需要按输入顺序遍历每个号码,遍历它保存在 vector 里面的所有子串,找出可行解即可。
AC代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn = 70000+5; 5 const int inf = 0x7fffffff; 6 map<string,int> num_p; 7 string num; 8 vector<string> subnum[maxn]; 9 int main(){ 10 ios::sync_with_stdio(false); 11 int n; 12 bool can; 13 cin>>n; 14 for(int t=1;t<=n;t++){ 15 cin>>num; 16 for(int i=1;i<=9;i++){ 17 for(int j=0;j<=9-i;j++){ 18 string temp=num.substr(j,i); 19 if(num_p[temp]==0){ 20 num_p[temp]=t; 21 subnum[t].push_back(temp); 22 } 23 else if(num_p[temp]!=t) num_p[temp]=-1; //有多个号码有这个子串,-1表示不可用 24 } 25 } 26 } 27 for(int t=1;t<=n;t++){ 28 for(int i=0;i<subnum[t].size();i++){ 29 if(num_p[subnum[t][i]]==t){ 30 cout<<subnum[t][i]<<endl; 31 break; 32 } 33 } 34 } 35 36 return 0; 37 }