codeforces Sereja and Algorithm

    xiaoxiao2021-03-25  31

    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.

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

    没看懂题意,其实人家说的暂停就是输出no 无语了我还以为是yes。小于区间长度直接yes。。题意是如果通过排列找到任意序列只要有不满足”zyx”, “xzy”, “yxz”. 就会暂停输出no。zyxzyxz 通过排列得知,当其中一个元素大于别的元素超过2,那么就会有不满足,就会输出no。还有就是不要在for判断条件处有strlen 会很慢,先弄个变量len=strlen(s)

    #include <bits/stdc++.h> using namespace std; char s[101010]; int x[101010]; int y[101010]; int z[101010]; int main() { scanf("%s",s); int m; scanf("%d",&m); int a=0,b=0,c=0; int len=strlen(s); for(int i=0;i<len;i++) { if(s[i]=='x') a++; if(s[i]=='y') b++; if(s[i]=='z') c++; x[i]=a; y[i]=b; z[i]=c; } int l,r; for(int i=0;i<m;i++) { scanf("%d %d",&l,&r); if(r-l+1<3) { printf("YES\n"); continue; } int aa=x[r-1]-x[l-2]; int bb=y[r-1]-y[l-2]; int cc=z[r-1]-z[l-2]; if(abs(aa-bb)<=1&&abs(aa-cc)<=1&&abs(bb-cc)<=1) printf("YES\n"); else printf("NO\n"); } }
    转载请注明原文地址: https://ju.6miu.com/read-26057.html

    最新回复(0)