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.
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).
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6
YES YES NO YES NO
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.
题意:
给定一组字符序列,问能否在给定的区间组成互不相邻的子串,
区间内的字符可以任意组合。
思路:
初次的思路:统计区间内最多和最少的字符个数,最多 - 最少 > 1的时候就是 NO,实际测试错误,
最后想想bug在两个最值的情况。。。(做题少)
后来参考了dp的方法,小看了codeforces的c.
AC CODE:
#include<stdio.h>
#include<cstring>
#include<algorithm>
#define AC main()
using namespace std;
const int MYDD = 1103 + 1e5;
char s[MYDD];
int dp[MYDD][4];
int AC {
int m;
scanf("%s %d", s+1, &m);
s[0] = ' ';
int n = strlen(s);
for(int i = 1; i < n; i++) {
for(int j = 0; j < 3; j++)
dp[i][j] = dp[i-1][j];
dp[i][s[i]-'x']++;
}
while(m--) {
int l, r, ans[4];
scanf("%d %d", &l, &r);
for(int j = 0; j < 3; j++)
ans[j] = dp[r][j] - dp[l-1][j];
if((abs(ans[0]-ans[1]) <= 1 && abs(ans[1]-ans[2]) <= 1 && abs(ans[2]-ans[0]) <= 1)||r-l < 2)
puts("YES");
else
puts("NO");
}
return 0;
}
BUG CODE:
#include<stdio.h>
#include<cstring>
#include<algorithm>
#define AC main()
using namespace std;
const int MYDD = 1103 + 1e5;
char s[MYDD];
int AC {
int m, l, r;
scanf("%s %d", s, &m);
while(m--) {
int a[3] = {0, 0, 0};//x y z
scanf("%d %d", &l, &r);
for(int j = l-1; j < r; j++) {
if(s[j] == 'x') a[0]++;
if(s[j] == 'y') a[1]++;
if(s[j] == 'z') a[2]++;
}
sort(a, a+3);
if((a[0] == a[1] && (a[2] - a[0]) == 1)||(a[1] == a[2]&&(a[1] - a[0]) == 1)||(a[0] == a[1] && a[1] == a[2]))
else printf("NO\n");
}
return 0;
}