您的位置:首页 > 编程语言 > Go语言

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

2016-10-06 19:00 441 查看
点击打开题目

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:

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.

Examples

input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6


output
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个字符的时候,某个字符的个数,然后对于不同的子串的长度进行判断就行了。

代码如下:

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: