CF1291D. Irreducible Anagrams

【题目链接】https://codeforces.com/contest/1291/problem/D

【题解】

一道非常神奇的字符串题。。。

考虑假设我们有一个串 $s$,我们想去构造它“互质”的 $t$ 串。

显然有种比较优的方式是把首尾的字符改变,并且把同种字符放在一起。

例如:$BACBABAC$ 可以构造 $AAACCBBB$。

打表找规律感性理解或分析性质可以得出以下结论:

1. $l=r$ 时一定存在“互质”串。

2. $s[l] eq s[r]$ 时一定存在“互质”串。

3. 字符种类大于 $3$ 时一定存在“互质”串。

直接用前缀和维护就好。效率 $O(26n+26q)$。

 1 #include<bits/stdc++.h>
 2 char s[300000];
 3 int n,cnt[200010][30];
 4 signed main()
 5 {
 6     scanf(" %s",s+1);n=strlen(s+1);
 7     for ( int i=1;i<=n;i++ ) for ( int j=1;j<=26;j++ ) cnt[i][j]=cnt[i-1][j]+(s[i]==j+96);
 8     int m;scanf("%d",&m);
 9     while ( m-- )
10     {
11         int l,r;scanf("%d%d",&l,&r);int ans=0;
12         for ( int i=1;i<=26;i++ ) if ( cnt[r][i]-cnt[l-1][i] ) ans++;
13         if ( l==r ) puts("Yes");
14         else if ( ans>=3 ) puts("Yes");
15         else if ( s[l]==s[r] ) puts("No");
16         else puts("Yes");
17     }
18     return 0;
19 }
CF1291D
原文地址:https://www.cnblogs.com/RenSheYu/p/12256084.html