题解:CF367A Sereja and Algorithm

推一下蒟蒻的 博客

题面

Sereja and Algorithm

题面翻译

给出一个字符串,元素只有 x x x, y y y, z z z 这三种,现给出一个区间 [ l , r ] [l,r] [l,r],这个区间内的字符可以任意调换,如果变换之后这个区间字符串的所有子串都是 zyxxzyyxz 的话,输出 yes(若 r − l < 2 r-l<2 rl<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:

  1. 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.
  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) (1lirin) 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}) (1m105) — 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) (1lirin) .

输出格式

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 rl<2 时,输出 YES

  • r − l ≥ 2 r - l \ge 2 rl2 时:

  1. 因为要多次用到区间内 x , y , z x,y,z x,y,z 的数量,考虑使用前缀计数。
  2. 如果变换之后这个区间字符串的所有子串都是 zyxxzyyxz,那么这个区间内 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;
}
  • 17
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值