BerOS File Suggestion (Codeforces 1070H)
阿新 • • 發佈:2018-11-11
題目連結: http://codeforces.com/problemset/problem/1070/H
思路:直接找的話,會時間超限,所以可以用map把上面輸入的串的所有字串都記下來,用兩個map,一個記錄有幾個串有匹配的,一個記錄匹配的串是哪個。
#include<cstdio> #include<iostream> #include <string> #include<cstring> #include <algorithm> #include <map> using namespace std; typedef long long LL; const int MAXN = 1e6; int main() { map<string,string> m1; //記錄與之匹配的是哪個串 map<string,int> m2; //記錄匹配的串的個數 int n;cin>>n; while(n--) { map<string,int> quchong; //去重用,因為在一個串中匹配多次的話,應該只記一次 string a;cin>>a; int len=a.size(); int i,j,k; for(i=0;i<len;i++) { for(j=1;j<=len;j++) { string t=a.substr(i,j); //擷取所有的字串 if(quchong[t]==0) { m1[t]=a; m2[t]++; quchong[t]=1; } } } } int q;cin>>q; while(q--) { string t;cin>>t; if(m2[t]==0) { cout<<"0 -"<<endl; } else { cout<<m2[t]<<" "<<m1[t]<<endl; } } return 0; }