Dsxv

gcj 2020 r1a

Apr 12th, 2020
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std ;
  3.  
  4. #define int long long
  5.  
  6. inline bool ok(string &a , string &b){
  7.     //cout << b.substr(0,(int)a.size()) << " " << a << endl ;
  8.     return b.substr(0,(int)a.size()) == a ;
  9. }
  10.  
  11. void solve(int t){
  12.     int n ;
  13.     cin >> n ;
  14.     string s[n] ;
  15.     for(int i = 0 ; i < n ; i++){
  16.         cin >> s[i] ;
  17.     }
  18.     map<int,set<string>> mp;
  19.     vector<string> last ;
  20.     for(int i = 0 ; i < n ; i++){
  21.         int cnt = 0 ;
  22.         string temp = "";
  23.         for(int j = 0 ; j < s[i].size() ; j++){
  24.             if(s[i][j] == '*'){
  25.                 mp[cnt++].insert(temp) ;
  26.                 temp = "";
  27.             } else {
  28.                 temp += s[i][j] ;
  29.             }
  30.         }
  31.         if(s[i].back() != '*') {
  32.             reverse(temp.begin(),temp.end()) ;
  33.             last.push_back(temp) ;
  34.         }
  35.     }
  36.     string check = "" ;
  37.     bool found = true ;
  38.     for(auto j : mp[0]){
  39.         if(j.size() > check.size()) {
  40.             check = j ;
  41.         }
  42.         if(!ok(j,check)) found = false ;
  43.     }
  44.     for(auto j : mp[0]) if(!ok(j,check)) found = false ;
  45.     string check2 = "";
  46.     for(auto j : last){
  47.         if(j.size() > check2.size()) {
  48.             check2 = j ;
  49.         }
  50.     }
  51.     for(auto j : last) if(!ok(j,check2)) found = false ;
  52.     reverse(check2.begin(),check2.end()) ;
  53.     string ans = "";
  54.     if(found){
  55.         for(auto i : mp){
  56.             if(i.first)
  57.             for(auto j : i.second){
  58.                 ans += j ;
  59.             }
  60.         }
  61.         ans = check + ans + check2 ;
  62.     } else {
  63.         ans = "*";
  64.     }
  65.     cout << "Case #"<< t << ": " << ans << '\n' ;
  66. }
  67.  
  68. int32_t main(){
  69.     ios :: sync_with_stdio(0) ;
  70.     cin.tie(0) ;
  71.     int t ;
  72.     cin >> t ;
  73.     for(int i = 1 ; i <= t ; i++){
  74.         solve(i) ;
  75.     }  
  76.     return 0 ;
  77. }
Add Comment
Please, Sign In to add comment