Kick Start 2022 Round A 2022 Palindrome Free Strings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| #include <bits/stdc++.h> using namespace std; bool oddpal(string& s, int r) { int l = r - 4; if(l < 0) return false; while(l < r) if(s[l++] != s[r--]) return false; return true; } bool evenpal(string& s, int r) { int l = r - 5; if(l < 0) return false; while(l < r) if(s[l++] != s[r--]) return false; return true; } bool manacher(string& s) { string p = "#"; for(auto ch : s) { p += string(1,ch) + '#'; } vector<int> dp(p.length()); int l = 0, r = -1; for(int i = 1; i < p.length() - 1; i++) { dp[i] = max(0, min(r - i, (l + r - i >= 0 ? dp[l + (r - i)] : -1))); while(i - dp[i] >= 0 and p[i + dp[i]] == p[i - dp[i]]) dp[i]++; if(r < i + dp[i]) { l = i - dp[i]; r = i + dp[i]; } if(p[i] != '#' and dp[i] >= 5) return false; else if(p[i] == '#' and dp[i] >= 6) return false; } return true; } bool solve(string& s, int i = 0) { if(i == s.length()) { return manacher(s); } if(s[i] != '?') { return !oddpal(s,i) and !evenpal(s,i) and solve(s, i + 1); } else { s[i] = '0'; if(!oddpal(s,i) and !evenpal(s,i) and solve(s, i + 1)) return true; s[i] = '1'; if(!oddpal(s,i) and !evenpal(s,i) and solve(s, i + 1)) return true; s[i] = '?'; return false; } }
int main() { int t; cin>>t; for(int i = 1; i <= t; i++) { int n; string s; cin>>n>>s; string res = solve(s) ? "POSSIBLE" : "IMPOSSIBLE"; cout<<"Case #"<<i<<": "<<res <<endl; } return 0; }
|