QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#547601 | #5119. Perfect Word | yuanyq5523 | WA | 7ms | 20520kb | C++17 | 2.1kb | 2024-09-04 23:39:44 | 2024-09-04 23:39:45 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=4e5+5;
int n,ch[N][30],cnt[N],idx=0;
int ne[N];
int len[N],fa[N],tr[N][30],last=1,tot=1,siz[N];
string a[N];
void add(int c){
int p=last;
int np=last=++tot;
len[np]=len[p]+1;
siz[np]=1;
for (;p && !tr[p][c];p=fa[p]) tr[p][c]=np;
if (!p) fa[np]=1;
else{
int q=tr[p][c];
if (len[p]+1==len[q]) fa[np]=q;
else{
int nq=++tot;
fa[nq]=fa[q];
len[nq]=len[q];
for (int j=0;j<26;j++) tr[nq][j]=tr[q][j];
len[nq]=len[p]+1;
fa[q]=fa[np]=nq;
for (;p && tr[p][c]==q;p=fa[p]) tr[q][c]=nq;
}
}
}
int calc(string s){
int sb=s.length();
for (int i=0;i<sb;i++) add(s[i]-'a');
int ans=0;
for (int i=2;i<=tot;i++) ans+=len[i]-len[fa[i]];
for (int i=0;i<=tot+3;i++){
for (int j=0;j<26;j++) tr[i][j]=0;
fa[i]=len[i]=siz[i]=0;
}
tot=1; last=1;
return ans;
}
void insert(string s){
int p=0;
for (int i=0;i<s.length();i++){
int j=s[i]-'a';
if (!ch[p][j]) ch[p][j]=++idx;
p=ch[p][j];
}
if (!cnt[p]) cnt[p]++;
}
void build(){
queue<int>q;
for (int i=0;i<26;i++){
if (ch[0][i]) q.push(ch[0][i]);
}
while (!q.empty()){
int u=q.front(); q.pop();
for (int i=0;i<26;i++){
int v=ch[u][i];
if (v){
ne[v]=ch[ne[u]][i];
q.push(v);
}
else ch[u][i]=ch[ne[u]][i];
}
}
}
int query(string s){
int ans=0;
vector<pair<int,int>>sb;
for (int k=0,i=0;k<s.length();k++){
i=ch[i][s[k]-'a'];
for (int j=i;j && cnt[j]>=0;j=ne[j]){
ans+=cnt[j];
sb.push_back({j,cnt[j]});
cnt[j]=-1;
}
}
for (auto uuu:sb) cnt[uuu.first]=uuu.second;
sb.clear();
return ans;
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n;
for (int i=1;i<=n;i++){
string s; cin>>s; a[i]=s;
insert(s);
}
build();
int ans=0;
string tmp;
for (int i=1;i<=n;i++){
int sb1=query(a[i]);
int sb2=calc(a[i]);
//cout<<sb1<<' '<<sb2<<endl;
if (sb1==sb2){
int t=a[i].length();
if (t>ans){
tmp=a[i];
ans=t;
}
}
}
//cout<<tmp<<endl;
cout<<ans<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 17160kb
input:
4 a t b ab
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 17264kb
input:
310 a aa aaa aaaa aaaaa aaaaaa aaaaaaa aaaaaaaa aaaaaaaaa aaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaaa aaaaaaaaaaaaa aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaa...
output:
300
result:
ok 1 number(s): "300"
Test #3:
score: -100
Wrong Answer
time: 7ms
memory: 20520kb
input:
4347 bbaaaab aabab bbaaaaababaaaba aababaaaabbbabababaaaba ababbabbbbbabbbabbab bbbbbbb bbbabbbabbabaab aabbbbabbbbaa aabaaabbbbbabaaababaa aaabababba aaababbaabab abbababbabbab bababaabbbbaaa aaaaaabaaaababaa ababaababba babaababbbababaaab bb abbbaaaababa b ab aaabbbbb abaabaaaaababbbab bbaaabaab b...
output:
15
result:
wrong answer 1st numbers differ - expected: '10', found: '15'