Irreducible Anagrams CodeForces - 1291D(前缀和)

2024-04-16 01:48

本文主要是介绍Irreducible Anagrams CodeForces - 1291D(前缀和),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Let’s call two strings s and t anagrams of each other if it is possible to rearrange symbols in the string s to get a string, equal to t.

Let’s consider two strings s and t which are anagrams of each other. We say that t is a reducible anagram of s if there exists an integer k≥2 and 2k non-empty strings s1,t1,s2,t2,…,sk,tk that satisfy the following conditions:

If we write the strings s1,s2,…,sk in order, the resulting string will be equal to s;
If we write the strings t1,t2,…,tk in order, the resulting string will be equal to t;
For all integers i between 1 and k inclusive, si and ti are anagrams of each other.
If such strings don’t exist, then t is said to be an irreducible anagram of s. Note that these notions are only defined when s and t are anagrams of each other.

For example, consider the string s= “gamegame”. Then the string t= “megamage” is a reducible anagram of s, we may choose for example s1= “game”, s2= “gam”, s3= “e” and t1= “mega”, t2= “mag”, t3= “e”:

On the other hand, we can prove that t= “memegaga” is an irreducible anagram of s.

You will be given a string s and q queries, represented by two integers 1≤l≤r≤|s| (where |s| is equal to the length of the string s). For each query, you should find if the substring of s formed by characters from the l-th to the r-th has at least one irreducible anagram.

Input
The first line contains a string s, consisting of lowercase English characters (1≤|s|≤2⋅105).

The second line contains a single integer q (1≤q≤105) — the number of queries.

Each of the following q lines contain two integers l and r (1≤l≤r≤|s|), representing a query for the substring of s formed by characters from the l-th to the r-th.

Output
For each query, print a single line containing “Yes” (without quotes) if the corresponding substring has at least one irreducible anagram, and a single line containing “No” (without quotes) otherwise.

Examples
Input
aaaaa
3
1 1
2 4
5 5
Output
Yes
No
Yes
Input
aabbbbbbc
6
1 2
2 4
2 2
1 9
5 7
3 5
Output
No
Yes
Yes
Yes
No
No
Note
In the first sample, in the first and third queries, the substring is “a”, which has itself as an irreducible anagram since two or more non-empty strings cannot be put together to obtain “a”. On the other hand, in the second query, the substring is “aaa”, which has no irreducible anagrams: its only anagram is itself, and we may choose s1= “a”, s2= “aa”, t1= “a”, t2= “aa” to show that it is a reducible anagram.

In the second query of the second sample, the substring is “abb”, which has, for example, “bba” as an irreducible anagram.

Sponsor

题意:
Anagrams意味着同样的字母组成,不同的顺序。
reducible Anagrams 意味着存在k≥2,使得两个属于Anagrams的两个串按照同一种分割方式分成k份,每一份都是Anagrams。

给你一个字符串,再指定一个子串S,要你求这个子串的一个anagrams串T,要求S和T无论如何如何分割,都不能组成reducible Anagrams

思路:
只有三种方式满足。

  1. l == r,此时k < 2,不符合。
  2. s[i] != s[r],此时只要把S串首尾互换得到T串,那么无论如何分割,首尾两个部分总是不符合的。
  3. 颜色大于三种。假设此时S串为 AXXXBXXXCXXXA(X代表位置)。那么T串只需要变成 CXXXAXXXAXXXB即可。同理无论如何分割首尾部分是不相同的。

串的数目可以用dp维护一个前缀和,dp[i][j]代表前i个序列为j的数目是多少。

#include <cstdio>
#include <cstring>
#include <algorithm>using namespace std;typedef long long ll;char s[300005];
int dp[300005][26];int main()
{scanf("%s",s + 1);int len = (int)strlen(s + 1);for(int i = 1;i <= len;i++){dp[i][s[i] - 'a']++;for(int j = 0;j <= 25;j++){dp[i][j] += dp[i - 1][j];}}int T;scanf("%d",&T);while(T--){int l,r;scanf("%d%d",&l,&r);int cnt = 0;for(int i = 0;i <= 25;i++){if(dp[r][i] - dp[l - 1][i])cnt++;}if(l == r)printf("Yes\n");else if(s[l] != s[r])printf("Yes\n");else if(cnt >= 3)printf("Yes\n");else printf("No\n");}return 0;
}

这篇关于Irreducible Anagrams CodeForces - 1291D(前缀和)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907502

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

【LeetCode热题100】前缀和

这篇博客共记录了8道前缀和算法相关的题目,分别是:【模版】前缀和、【模版】二维前缀和、寻找数组的中心下标、除自身以外数组的乘积、和为K的子数组、和可被K整除的子数组、连续数组、矩阵区域和。 #include <iostream>#include <vector>using namespace std;int main() {//1. 读取数据int n = 0, q = 0;ci

Codeforces Round 971 (Div. 4) (A~G1)

A、B题太简单,不做解释 C 对于 x y 两个方向,每一个方向至少需要 x / k 向上取整的步数,取最大值。 由于 x 方向先移动,假如 x 方向需要的步数多于 y 方向的步数,那么最后 y 方向的那一步就不需要了,答案减 1 代码 #include <iostream>#include <algorithm>#include <vector>#include <string>

前缀和 — 利用前缀信息解决子数组问题

【前缀和的核心思想是预先处理数组来快速计算任意子数组的和,基本上用于数组和序列问题。】 前缀和算法具体步骤 构造前缀和数组: 给定一个数组nums,其前缀和数组prex定义为prex[i]表示为数组nums从起始位置到第i个位置的元素累加和。构建前缀和公式: p r e x [ i ] = n u m s [ i ] ( i = = 0 ) p r e x [ i ] = p r e x

Codeforces#295(Div.2)A、B(模拟+BFS)

解题报告链接:点击打开链接 C. 题目链接:点击打开链接 解题思路: 对于给定的字符串,取出现次数最多的字母(可以同时有多个)。由这些字母组成长度为n的字符串,求有多少种组合。最后用数学知识即可。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <climits>

Codeforces Round #281 (Div. 2)A(构造+暴力模拟)

题目链接:http://codeforces.com/problemset/problem/493/A 解题思路: 暴力的判断,分三种情况去判断即可。注意如果之前已经被罚下场后,那么在后面的罚下情况不应该算在输出结果内。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <co