Codeforces 368C Sereja and Algorithm【思维】

来源:互联网 发布:linux编辑保存命令 编辑:程序博客网 时间:2024/06/05 14:57

A. 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 asq = 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". Ifq 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 substringslisli + 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 exceed105. It is guaranteed that strings only contains characters: 'x', 'y', 'z'.

The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Nextm 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
zyxxxxxxyyz55 51 31 111 43 6
Output
YESYESNOYESNO
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. 


题目大意:

给你一个字符串,有m个询问,每次询问一个区间,我们可以将区间内所有字符重新排序,使得如果最终从中任意选择连续三个字符的子串都是"zyx", "xzy", "yxz"中的一种那么输出YES,否则输出NO。

如果长度小于3,明显输出YES、


思路:


因为这三个串:

zyx,xzy,yxz明显是根据挪一位字符的位子就可以互相得到的,那么很明显,如果我们的区间字符串的三种字符的个数如果最多出现的次数-最少出现的次数<=1,我们就能将其转换成目标串,也就是输出YES.


Ac代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;char a[100500];int x[100500];int y[100500];int z[100500];int main(){    while(~scanf("%s",a))    {        int n=strlen(a);        for(int i=0;i<n;i++)        {            if(i==0)            {                x[i]=0;                y[i]=0;                z[i]=0;            }            else            {                x[i]=x[i-1];                y[i]=y[i-1];                z[i]=z[i-1];            }            if(a[i]=='x')x[i]++;            if(a[i]=='y')y[i]++;            if(a[i]=='z')z[i]++;        }        int q;        scanf("%d",&q);        while(q--)        {            int l,r;            scanf("%d%d",&l,&r);            if(r-l<=1)printf("YES\n");            else            {                int contx,conty,contz;                r--;                l--;                if(l==0)                {                    contx=x[r];                    conty=y[r];                    contz=z[r];                }                else                {                    contx=x[r]-x[l-1];                    conty=y[r]-y[l-1];                    contz=z[r]-z[l-1];                }                int c[3];                c[0]=contx;c[1]=conty;c[2]=contz;                sort(c,c+3);                if(c[2]-c[0]<=1)printf("YES\n");                else printf("NO\n");            }        }    }}








0 0
原创粉丝点击