推一下蒟蒻的 博客
题面
Sereja and Algorithm
题面翻译
给出一个字符串,元素只有
x
x
x,
y
y
y,
z
z
z 这三种,现给出一个区间
[
l
,
r
]
[l,r]
[l,r],这个区间内的字符可以任意调换,如果变换之后这个区间字符串的所有子串都是 zyx
或 xzy
或 yxz
的话,输出 yes
(若
r
−
l
<
2
r-l<2
r−l<2 也输出 YES
),否则输出 NO
。
题目描述
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 = q 1 q 2 . . . q k q=q_{1}q_{2}...\ q_{k} q=q1q2... qk . The algorithm consists of two steps:
- Find any continuous subsequence (substring) of three characters of string
q
q
q , which doesn’t equal to either string
zyx
,xzy
,yxz
. If q q 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 q 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 = s 1 s 2 . . . s n s=s_{1}s_{2}...\ s_{n} s=s1s2... sn , consisting of n n n characters. The boy conducts a series of m m m tests. As the i i i -th test, he sends substring s l i s l i + 1... s r i s_{li}s_{li}+1...\ s_{ri} slisli+1... sri ( 1 ≤ l i ≤ r i ≤ n ) (1 \le l_{i} \le r_{i} \le n) (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 ( l i , r i ) (l_{i},r_{i}) (li,ri) determine if the algorithm works correctly on this test or not.
输入格式
The first line contains non-empty string s s s , its length ( n n n ) doesn’t exceed 1 0 5 10^{5} 105 . It is guaranteed that string s s s only contains characters: ‘x’, ‘y’, ‘z’.
The second line contains integer m m m ( 1 ≤ m ≤ 1 0 5 ) (1 \le m \le 10^{5}) (1≤m≤105) — the number of tests. Next m m m lines contain the tests. The i i i -th line contains a pair of integers l i l_{i} li , r i r_{i} ri ( 1 ≤ l i ≤ r i ≤ n ) (1 \le l_{i} \le r_{i} \le n) (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.
样例 #1
样例输入 #1
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
样例输出 #1
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.
题解
思路
-
当 r − l < 2 r - l < 2 r−l<2 时,输出
YES
。 -
当 r − l ≥ 2 r - l \ge 2 r−l≥2 时:
- 因为要多次用到区间内 x , y , z x,y,z x,y,z 的数量,考虑使用前缀计数。
- 如果变换之后这个区间字符串的所有子串都是
zyx
或xzy
或yxz
,那么这个区间内 x , y , z x,y,z x,y,z 的数量差不超过 1 1 1。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t, l, r, cntx[100005], cnty[100005], cntz[100005];
string s;
signed main() {
cin.tie(0), cout.tie(0);
cin >> s >> t;
s = '$'+s ;
for (int i = 1; i < s.size(); i++) {// 前缀计数
cntx[i] = cntx[i - 1] + (s[i] == 'x' ? 1 : 0);
cnty[i] = cnty[i - 1] + (s[i] == 'y' ? 1 : 0);
cntz[i] = cntz[i - 1] + (s[i] == 'z' ? 1 : 0);
}
while (t--) {
cin >> l >> r;
if (r - l < 2) { //输出 YES
cout << "YES\n";
continue;
}
int x = cntx[r] - cntx[l - 1], y = cnty[r] - cnty[l - 1], z = cntz[r] - cntz[l - 1];// x,y,z 的数量
if (abs(x - y) \le 1 && abs(y - z) \le 1 && abs(x - z) \le 1) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}