Time Limit: 1000MS | Memory Limit: 262144KB | 64bit IO Format: %I64d & %I64u |
Description
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:
- 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.
- 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.
Sample Input
zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6
YES YES NO YES NO
Hint
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.
Source
Codeforces Round #215 (Div. 2)
题意:题意有点坑,不是很理解,但是什么时候输出YES跟NO还是知道的,当字符串的长度不够3的时候输出YES,当字符串排列之后可以使得每三个连续的字符是"zyx","yxz","xzy"的时候输出YES,其他情况输出NO
思路:第一种情况没啥,关键是第二个,开始推理,先看"zyx",这时候x,y,z的个数是一样的,因为字符串的字符之间的共用关系,所以我添加一个z,这时候我们得到了第二个字符串"yxz",这时候平衡也被打破,z的个数加一,然后我们再加一个y,又得到了"xzy",y的个数又加一,最后加z又得到一个,这时候x,y,z,的个数平衡了,从添加的过程中我们可以看出x,y,z,他们的个数相差最多1,然后再添加又会平衡,所以得到第二个规律
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 100000+10
char s[MAXN];
int x[MAXN], y[MAXN], z[MAXN];
int main()
{
while (cin >> s)
{
int n;
cin >> n;
int xx, yy, zz;
xx = yy = zz = 0;
for (int i = 0; i < strlen(s); i++)
{
if (s[i] == 'x')
xx++;
if (s[i] == 'y')
yy++;
if (s[i] == 'z')
zz++;
z[i] = zz, x[i] = xx, y[i] = yy;
}
int l, r;
for (int i = 0; i < n;i++)
{
cin >> l >> r;
if (r - l + 1 < 3)
cout << "YES" << endl;
else
{
int a = x[r - 1] - x[l - 2];
int b = y[r - 1] - y[l - 2];
int c = z[r - 1] - z[l - 2];
if (abs(a-b) <= 1 && abs(a-c) <= 1 && abs(b-c) <= 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
memset(s,0,sizeof(s));
}
return 0;
}