Advertisement
ccbeginner

code jam 2020 round 1A Pattern Matching

Apr 10th, 2020
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. //code jam 2020 round 1A Pattern Matching
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. string arr[50];
  6. string prefix, suffix;
  7. bool have_ans;
  8.  
  9. bool check(string a, string b){
  10.     for(int i = 0; i < min(a.size(), b.size()); ++i){
  11.         if(a[i] != b[i])return false;
  12.     }
  13.     return true;
  14. }
  15.  
  16. void update(string s){
  17.     string pre = "", suf = "";
  18.     for(int i = 0; i < s.size() && s[i] != '*'; ++i)pre += s[i];
  19.     for(int i = s.size()-1; i >= 0 && s[i] != '*'; --i)suf += s[i];
  20.     if(check(prefix, pre) && check(suffix, suf)){
  21.         if(pre.size() > prefix.size())prefix = prefix = pre;
  22.         if(suf.size() > suffix.size())suffix = suffix = suf;
  23.     }else have_ans = false;
  24. }
  25.  
  26. int main(){
  27.     int t;
  28.     cin >> t;
  29.     for(int c = 1; c <= t; ++c){
  30.         prefix = suffix = "";
  31.         have_ans = 1;
  32.         int n;
  33.         cin >> n;
  34.         for(int i = 0; i < n; ++i){
  35.             cin >> arr[i];
  36.             update(arr[i]);
  37.             //cout << prefix << ' ' << suffix << ' ' << have_ans << '\n';
  38.         }
  39.         cout << "Case #" << c << ": ";
  40.         if(!have_ans)cout << '*';
  41.         else{
  42.             cout << prefix;
  43.             for(int i = 0; i < n; ++i){
  44.                 for(int j = 0; j < arr[i].size(); ++j){
  45.                     if(arr[i][j] != '*')cout << arr[i][j];
  46.                 }
  47.             }
  48.             for(int i = suffix.size()-1; i >= 0; --i)cout << suffix[i];
  49.         }
  50.         cout << endl;
  51.     }
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement