首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【CodeForces】368C - Sereja and Algorithm(思维)

【CodeForces】368C - Sereja and Algorithm(思维)

作者头像
FishWang
发布2025-08-26 14:39:58
发布2025-08-26 14:39:58
1930
举报

点击打开题目

C. Sereja and Algorithm

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:

  1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
  2. Rearrange the letters of the found subsequence randomly and go to step 1.

Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.

Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.

Input

The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.

The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).

Output

For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.

Examples

input

代码语言:javascript
复制
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6

output

代码语言:javascript
复制
YES
YES
NO
YES
NO

Note

In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.

读了半天的题。。。

就是说给出一个子串,如果可以变成任意一个串的连续三个字符都是"zyx", "xzy", "yxz"其中的一个,输出YES,否则输出NO。

求出到第n个字符的时候,某个字符的个数,然后对于不同的子串的长度进行判断就行了。

代码如下:

代码语言:javascript
复制
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
int main()
{
	char str[100000+11];
	int ant[3][100000+11];
	int m;
	int l,r;
	scanf ("%s",str+1);
	ant[0][0] = ant[1][0] = ant[2][0] = 0;
	for (int i = 1 ; str[i] ; i++)
	{
		if (str[i] == 'x')
		{
			ant[0][i] = ant[0][i-1] + 1;
			ant[1][i] = ant[1][i-1];
			ant[2][i] = ant[2][i-1];
		}
		else if (str[i] == 'y')
		{
			ant[0][i] = ant[0][i-1];
			ant[1][i] = ant[1][i-1] + 1;
			ant[2][i] = ant[2][i-1];
		}
		else
		{
			ant[0][i] = ant[0][i-1];
			ant[1][i] = ant[1][i-1];
			ant[2][i] = ant[2][i-1] + 1;
		}
	}
	scanf ("%d",&m);
	while (m--)
	{
		scanf ("%d %d",&l,&r);
		int n = r - l + 1;
		if (n < 3)		//数量小于3的直接输出YES 
		{
			puts("YES");
			continue;
		}
		int a[4];
		a[0] = ant[0][r] - ant[0][l-1];
		a[1] = ant[1][r] - ant[1][l-1];
		a[2] = ant[2][r] - ant[2][l-1];
		sort(a,a+3);
		if (n % 3)
		{
			if (a[2] == n / 3 + 1 && a[0] == n / 3)
				puts("YES");
			else
				puts("NO");
		}
		else
		{
			if (a[0] == a[2])
				puts("YES");
			else
				puts("NO");
		}
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档