QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#565277 | #8475. Palindrome Strings | ucup-team1231# | ML | 0ms | 0kb | C++14 | 2.1kb | 2024-09-15 20:48:56 | 2024-09-15 20:48:58 |
answer
#include <bits/stdc++.h>
using namespace std;
int n, m;
string S[100005], T[100005];
int tre[1000005][2], fail[1000005], nxt[1000005][2], cnt;
bool del[1000005];
vector<int> hv[1000005], thv[1000005];
int ins(const string& A) {
int v = 0;
for(auto c : A) {
int& nx = tre[v][c - '0'];
if(!nx) nx = cnt++;
v = nx;
}
return v;
}
void bfs(int v) {
queue<int> que;
que.push(v);
while(!que.empty()) {
int x = que.front(); que.pop();
del[x] |= del[fail[x]];
for(auto i : hv[fail[x]]) {
if(hv[x].size() >= 2) break;
hv[x].push_back(i);
}
for(int i = 0; i < 2; i++) if(tre[x][i]) {
int y = tre[x][i];
fail[y] = x == 0 ? 0 : nxt[fail[x]][i];
nxt[x][i] = y;
que.push(y);
} else nxt[x][i] = nxt[fail[x]][i];
}
}
bool dfs1(int v, int c) {
if(del[v]) return false;
if(hv[v].size() >= 2 || (hv[v].size() == 1 && hv[v][0] != c)) return true;
if(thv[v].size() >= 2 || (thv[v].size() == 1 && thv[v][0] == c)) return false;
thv[v].push_back(v);
for(int i = 0; i < 2; i++) if(dfs1(nxt[v][i], c)) return true;
return false;
}
bool dfs0(int v) {
if(del[v]) return false;
for(auto i : hv[v]) if(dfs1(v, i)) return true;
for(int i = 0; i < 2; i++) if(dfs0(nxt[v][i])) return true;
return false;
}
void solve() {
cin >> n;
for(int i = 1; i <= n; i++) cin >> S[i];
for(int i = 1; i <= m; i++) cin >> T[i];
cnt = 1;
for(int i = 1; i <= n; i++) hv[ins(S[i])].push_back(i);
for(int i = 1; i <= m; i++) del[ins(T[i])] = true;
bfs(0);
if(dfs0(0)) printf("Yes\n");
else printf("No\n");
for(int i = 0; i < cnt; i++) {
tre[i][0] = tre[i][1] = nxt[i][0] = nxt[i][1] = fail[i] = 0;
del[i] = false;
hv[i].clear(); thv[i].clear();
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Memory Limit Exceeded
input:
8 3 icpccamp p c pc