QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#431833 | #6851. Cyclically Isomorphic | heaksicn | AC ✓ | 117ms | 10688kb | C++17 | 1.4kb | 2024-06-06 10:00:09 | 2024-06-06 10:00:09 |
Judging History
answer
#include<cstdio>
#include<map>
#include<iostream>
using namespace std;
typedef long long ll;
inline ll read(){
ll x=0;
int f=0,ch=0;
while(ch<48||ch>57) f=(ch=='-'),ch=getchar();
while(ch>47&&ch<58) x=(x<<3)+(x<<1)+(ch&15),ch=getchar();
return f?-x:x;
}
inline void write(ll x,char end=' '){
if(x==0){
putchar('0');
putchar(end);
return;
}
if(x<0) putchar('-'),x=-x;
int ch[40]={0},cnt=0;
while(x){
ch[cnt++]=(int)(x%10);
x/=10;
}
while(cnt--) putchar(ch[cnt]+48);
putchar(end);
}
const int N=1e5+5;
const ll mod=1e13+7;
int n,m;
string s;
int fa[N];
map<ll,int> mp;
ll base[N];
inline ll hsh(){
ll h=0;
for(int i=0;i<m;++i)
h=(h*37%mod+s[i]-'a'+1)%mod;
return h;
}
int main(){
base[0]=1;
for(int i=1;i<N;++i) base[i]=base[i-1]*37%mod;
int T=read();
while(T--){
n=read(),m=read();
mp.clear();
for(int i=1;i<=n;++i) fa[i]=i;
for(int i=1;i<=n;++i){
cin>>s;
ll t=hsh();
// printf("%lld ",t);
if(mp[t])
fa[i]=fa[mp[t]];
else{
mp[t]=fa[i];
for(int j=0;j<m-1;++j){
int c=s[j]-'a'+1;
t=(t-c*base[m-1]%mod)%mod;
t=(t+mod)%mod;
t=(t*37%mod+c)%mod;
// printf("%lld ",t);
mp[t]=fa[i];
}
}
// puts("");
}
int q=read();
while(q--){
int x=read(),y=read();
if(fa[x]==fa[y]) puts("Yes");
else puts("No");
}
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 117ms
memory: 10688kb
input:
5 10 10000 afcadffafeeebdfedfdafecfdbeecbcadfedcffaedecbacffedeccacfcbfbbdbcefbdefefebcbeacfbaafddbabcbfaeddaaefebbddafaaaaaedcbbdaafaffcbeeebfbbeedebabfbdbaceabfcccfedbdeafccbaffdeadbefbfeadbbedeeabbaafebaceeaeedbaddfecdefaabdbcfadadaebbdeedcfeeabbbdbcefdadbeecafcabbddddbcacadbbcddeddbbeecbdadefbaa...
output:
No No No Yes Yes No No No Yes No No No No No No No No No No No No No Yes No No No No No No No No No No Yes No No No Yes No No No No No No No No Yes No Yes No No No No No Yes No No No No No No No No No Yes No No Yes No No No No No No No No No No No Yes No Yes No Yes No No No No No No No No Yes Yes Ye...
result:
ok 500000 lines