当前位置: 首页 > 工具软件 > A* Algorithm > 使用案例 >

codeforces --A - Sereja and Algorithm

柴翰藻
2023-12-01

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 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.

Sample test(s)
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"子串的字符串,长度小于3也认为是符合的,然后你只需要 统计询问的串中x,y,z的个数即可,只要两两相差小于等于1就可以了。。其实也算水题了。。还有就是注意边界的问题,不过没注意的好像也可以过。。下面贴下源码:



#include<stdio.h>
#include<string.h>
int abs(int a)
{
    return a>0?a:-a;
}
int x[100005],y[100005],z[100005];
char str[100005];
int main()
{
    int lenth;
    scanf("%s",str);
        lenth=strlen(str);
        int c_x,c_y,c_z;
        c_x=c_y=c_z=0;
        for(int i=0;i<lenth;i++)
        {
            if(str[i]=='x')c_x++;
            else if(str[i]=='y')c_y++;
            else c_z++;
            x[i]=c_x;
            y[i]=c_y;
            z[i]=c_z;
        }
    int m,l,r;
    scanf("%d",&m);
    int rx,ry,rz;
    for(int i=0;i<m;i++)
    {
        scanf("%d %d",&l,&r);
        if(r-l<2)printf("YES\n");
        else{
            if(l==1)
            {
                  rx=x[r-1];
                  ry=y[r-1];
                  rz=z[r-1];
            }
            else
            {
                rx=x[r-1]-x[l-2];
                ry=y[r-1]-y[l-2];
                rz=z[r-1]-z[l-2];
            }
           if(abs(rx-ry)<=1&&abs(rx-rz)<=1&&abs(ry-rz)<=1)
           printf("YES\n");
           else printf("NO\n");
         }
    }
    return 0;
}


 类似资料: