Codeforces 367A Sereja and Algorithm 【规律】

题目链接:Codeforces 367A Sereja and Algorithm

A. Sereja and Algorithm
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

题意:给定一个串,问你[l, r]里面的子串是否存在一个排列使得任意三个连续的字符均属于’zyx’、’xzy’、’yxz’三个串的一个。

思路:构成一个最长的zyxzyxz,发现只有最多的字符-最小的字符 <= 1即可。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e5 + 10;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
int x[MAXN], y[MAXN], z[MAXN];
char str[MAXN];
int main()
{
    scanf("%s", str+1);
    int len = strlen(str+1);
    x[0] = y[0] = z[0] = 0;
    for(int i = 1; i <= len; i++) {
        x[i] = x[i-1] + (str[i] == 'x');
        y[i] = y[i-1] + (str[i] == 'y');
        z[i] = z[i-1] + (str[i] == 'z');
    }
    int m; scanf("%d", &m);
    while(m--) {
        int l, r; scanf("%d%d", &l, &r);
        if(r - l + 1 < 3) {
            printf("YES\n");
            continue;
        }
        bool flag = true;
        int sumx, sumy, sumz;
        sumx = x[r] - x[l-1];
        sumy = y[r] - y[l-1];
        sumz = z[r] - z[l-1];
        if(sumx == 0 || sumy == 0 || sumz == 0) {
            flag = false;
        }
        else if(max(sumx, max(sumy, sumz)) - min(sumx, min(sumy, sumz)) > 1) {
            flag = false;
        }
        printf(flag ? "YES\n" : "NO\n");
    }
    return 0;
}

你可能感兴趣的:(Codeforces 367A Sereja and Algorithm 【规律】)