标签:lang class center nbsp sig unsigned 相等 lin out
第一行一个字符串s(1<=|s|<=300000) 第二行一个整数T(1<=T<=300000) 对于每次询问: 第一行5个整数k,l1,r1,l2,r2,表示有k个等价规则,询问的是子串[l1,r1],[l2,r2](1<=k<=10,1<=l1<=r1<=|s|,1<=l2<=r2<=|s|) 接下来k行每行两个连续的字符表示这两个字符等价。 此题中所有的字符均为小写字母。
T行,若相似则输出“YES”否则输出“NO”
abac 3 1 1 2 3 4 bc 1 1 2 3 4 ac 1 1 2 2 3 ac
YES YES NO
#include <bits/stdc++.h> inline long long read(){long long x=0,f=1;char ch=getchar();while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}return x*f;} using namespace std; typedef unsigned long long ULL; const int N = 4e5+10; const ULL mod = 10000019ULL; ULL sqr[N], tmp = 1, pre[N][27], fi[50], se[50]; map<ULL, int> mp; char s[N], ch[50]; int T, fa[50], vis[50], F[50]; int finds(int x) {return fa[x] == x? x:fa[x]=finds(fa[x]);} int main() { scanf("%s%d",s+1,&T); int n = strlen(s+1); sqr[0] = 1; for(int i = 1; i <= n; ++i) sqr[i] = sqr[i-1] * mod, mp[sqr[i]] = 1,mp[(-sqr[i])] = 1; for(int i = 1; i <= n; ++i) { tmp = tmp * mod; pre[i][s[i] - ‘a‘ + 1] += tmp; } for(int i = 1; i <= n; ++i) { for(int j = 1; j <= 26; ++j) pre[i][j] += pre[i-1][j]; } for(int t = 1; t <= T; t++) { int k,l1,r1,l2,r2,cnt = 0; scanf("%d%d%d%d%d",&k,&l1,&r1,&l2,&r2); for(int i = 1; i <= 26; ++i) fa[i] = i,vis[i] = 0; for(int i = 1; i <= 26; ++i) fi[i] = pre[r1][i] - pre[l1-1][i]; for(int i = 1; i <= 26; ++i) se[i] = pre[r2][i] - pre[l2-1][i]; for(int i = 1; i <= k; ++i) { scanf("%s",ch); int fx = finds(ch[0] - ‘a‘ + 1); int fy = finds(ch[1] - ‘a‘ + 1); if(fx > fy) swap(fx,fy); if(fx != fy) fa[fy] = fx,fi[fx] += fi[fy],se[fx] += se[fy],fi[fy] = 0,se[fy] = 0; } if(r1 - l1 != r2 - l2) { printf("NO\n"); continue; } for(int i = 1; i <= 26; ++i) { int nfa = finds(i); if(fi[nfa]*sqr[l2 - l1] != se[nfa] && !vis[nfa]) F[++cnt] = i, vis[nfa] = 1; } if(cnt == 0) printf("YES\n"); else { if(cnt > 2) printf("NO\n"); else { if(fi[F[1]]*sqr[l2 - l1] + fi[F[2]]*sqr[l2 - l1]== se[F[2]] + se[F[1]]) { if(mp[(fi[F[1]]*sqr[l2 - l1] - se[F[1]])])printf("YES\n"); else printf("NO\n"); } else printf("NO\n"); } } } return 0; }
标签:lang class center nbsp sig unsigned 相等 lin out
原文地址:http://www.cnblogs.com/zxhl/p/7491541.html