#include<iostream>
#include<algorithm>
#include<vector>
#include<utility>
#include<set>
#include<string>
using namespace std;
#define ll long long
vector<int> dsu;
const ll mod=998244353;
vector<string> vec;
void build(int n){
dsu.resize(n+1);
for(int i=1;i<=n;i++) dsu[i]=i;
}
int query(int x){
if(x==dsu[x]) return x;
int tmp=query(dsu[x]);
dsu[x]=tmp;
return tmp;
}
void unite(int a,int b){
a=query(a),b=query(b);
if(a==b) return;
vec[a]+=vec[b];
dsu[b]=a;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
string s;
cin>>s;
build(n);
vec.resize(n+1,"");
for(int i=0;i<n;i++) vec[i+1]+=s[i];
int a,b;
vector<ll> bases(1005);
bases[0]=1;
for(int i=1;i<1005;i++) bases[i]=bases[i-1]*3%mod;
for(int i=1;i<n;i++){
cin>>a>>b;
unite(a,b);
string now=vec[query(a)];
int sz=now.size();
vector<ll> hash(sz+1),rev(sz+1);
hash[0]=1,rev[0]=1;
for(int i=1;i<=sz;i++){
hash[i]=(hash[i-1]*3%mod+(now[i-1]-'0'+1))%mod;
rev[i]=(rev[i-1]*3%mod+(now[sz-i]-'0'+1))%mod;
}
set<ll> s;
for(int i=1;i<=sz;i++){
for(int j=i;j<=sz;j++){
if((mod+hash[j]-hash[i-1]*bases[j-i+1]%mod)%mod==(mod+rev[sz-i+1]-rev[sz-j]*bases[j-i+1]%mod)%mod){
s.insert((mod+hash[j]-hash[i-1]*bases[j-i+1]%mod)%mod);
}
}
}
cout<<s.size()<<"\n";
//or(auto x:s) cout<<x<<" ";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
727 ms |
592 KB |
Output is correct |
16 |
Correct |
148 ms |
592 KB |
Output is correct |
17 |
Correct |
254 ms |
592 KB |
Output is correct |
18 |
Correct |
224 ms |
592 KB |
Output is correct |
19 |
Execution timed out |
1029 ms |
852 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1038 ms |
4520 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
727 ms |
592 KB |
Output is correct |
16 |
Correct |
148 ms |
592 KB |
Output is correct |
17 |
Correct |
254 ms |
592 KB |
Output is correct |
18 |
Correct |
224 ms |
592 KB |
Output is correct |
19 |
Execution timed out |
1029 ms |
852 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |