QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#431368#6851. Cyclically Isomorphicucup-team3591#AC ✓114ms10644kbC++141.4kb2024-06-05 13:47:322024-06-05 13:47:32

Judging History

你现在查看的是最新测评结果

  • [2024-06-05 13:47:32]
  • 评测
  • 测评结果:AC
  • 用时:114ms
  • 内存:10644kb
  • [2024-06-05 13:47:32]
  • 提交

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: 114ms
memory: 10644kb

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