Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //code jam 2020 round 1A Pattern Matching
- #include<bits/stdc++.h>
- using namespace std;
- string arr[50];
- string prefix, suffix;
- bool have_ans;
- bool check(string a, string b){
- for(int i = 0; i < min(a.size(), b.size()); ++i){
- if(a[i] != b[i])return false;
- }
- return true;
- }
- void update(string s){
- string pre = "", suf = "";
- for(int i = 0; i < s.size() && s[i] != '*'; ++i)pre += s[i];
- for(int i = s.size()-1; i >= 0 && s[i] != '*'; --i)suf += s[i];
- if(check(prefix, pre) && check(suffix, suf)){
- if(pre.size() > prefix.size())prefix = prefix = pre;
- if(suf.size() > suffix.size())suffix = suffix = suf;
- }else have_ans = false;
- }
- int main(){
- int t;
- cin >> t;
- for(int c = 1; c <= t; ++c){
- prefix = suffix = "";
- have_ans = 1;
- int n;
- cin >> n;
- for(int i = 0; i < n; ++i){
- cin >> arr[i];
- update(arr[i]);
- //cout << prefix << ' ' << suffix << ' ' << have_ans << '\n';
- }
- cout << "Case #" << c << ": ";
- if(!have_ans)cout << '*';
- else{
- cout << prefix;
- for(int i = 0; i < n; ++i){
- for(int j = 0; j < arr[i].size(); ++j){
- if(arr[i][j] != '*')cout << arr[i][j];
- }
- }
- for(int i = suffix.size()-1; i >= 0; --i)cout << suffix[i];
- }
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement