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.

題意:
給出一個字符串, 給出多組區間, 問能否把這個區間拿出來重新排列, 使得這個區間任意三個連續字符是enum{“zyx”, “xzy”, “yxz”}, 其中一種.

解題思路:
能滿足上述條件, 那麼就記錄x, y, z 的個數.
假設A == B+1, 那麼三個的形式就應該是AAA AAB ABB這種形式, 因爲有多組查詢, 所以用前綴和維護出現的次數.

AC代碼:

#include <bits/stdc++.h>
using namespace std;
string a;
const int maxn = 1e5+5;
int cnt[maxn][3];
int main()
{
    ios::sync_with_stdio(0);
    cin >> a;
    for(int i = 0; a[i]; i++){
        cnt[i+1][a[i] - 'x']++;
        for(int j = 0; j < 3; j++)
            cnt[i+1][j] += cnt[i][j];
    }
    int n;
    cin >> n;
    while(n--){
        int l, r;
        cin >> l >> r;
        l, r;
        if(r-l+1 < 3){
            cout << "YES" << endl;
            continue;
        }
        int num[3];
        for(int i = 0; i < 3; i++)
            num[i] = cnt[r][i] - cnt[l-1][i];
        sort(num, num+3);
        if((num[0] == num[1] && num[1] == num[2]) || (num[2] == num[1] && num[2] == num[0] + 1) || (num[2] == num[1] + 1 && num[2] == num[0] + 1))
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章