【codeforces 367A Sereja and Algorithm】+ 预处理
来源:互联网 发布:淘宝食品备案 编辑:程序博客网 时间:2024/06/05 19:53
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.
AC代码 :
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char st[100011];int dp[4][100011];int main(){ int T,a,b,nx,ny,nz,lx,ly,lz,nl; scanf("%s",st + 1); memset(dp,0,sizeof(dp)); int pl = strlen(st + 1); nx = ny = nz = 0; for(int i = 1 ; i <= pl ; i++){ if(st[i] == 'x') nx++; else if(st[i] == 'y') ny++; else nz++; dp[1][i] = nx; dp[2][i] = ny; dp[3][i] = nz; } scanf("%d",&T); while(T--) { scanf("%d%d",&a,&b); a--; if(b - a < 3) printf("YES\n"); else{ nl = 1; lx = dp[1][b] - dp[1][a]; ly = dp[2][b] - dp[2][a]; lz = dp[3][b] - dp[3][a]; if(abs(lx - ly) > 1 || abs(ly - lz) >1 || abs(lx - lz) >1) nl = 0; if(nl) printf("YES\n"); else printf("NO\n"); } } return 0;}
- 【codeforces 367A Sereja and Algorithm】+ 预处理
- Codeforces 367A Sereja and Algorithm
- CodeForces 367A-Sereja and Algorithm【规律】
- Codeforces 367A Sereja and Algorithm 【规律】
- CodeForces 367A Sereja and Algorithm
- codeforces --A - Sereja and Algorithm
- codeforces Sereja and Algorithm
- Sereja and Algorithm -----Codeforces Round #215 div.1 A
- 【瞎搞】 Codeforces Round #215 (Div. 1) A Sereja and Algorithm
- Codeforces Round #215_div2_C. Sereja and Algorithm
- Codeforces 368C Sereja and Algorithm【思维】
- 【codeforces 368B Sereja and Suffixes】+ 预处理
- A. Sereja and Algorithm----思维+贪心
- CF-Sereja and Algorithm
- Sereja and Algorithm
- CodeForces 315A Sereja and Bottles
- Codeforces 380 A. Sereja and Prefixes
- CodeForces 425A Sereja and Swaps
- 新手报道
- 百度音乐Api简单简单实用实例及封装
- HashMap的resize都干了啥
- iOS 企业 ipa发布方法
- 王朝 第五周
- 【codeforces 367A Sereja and Algorithm】+ 预处理
- Form表单提交数据的几种方式
- 王朝 第五周
- Prometheus
- HDU 2503 a/b + c/d
- VMware Ubuntu安装详解
- http://blog.csdn.net/shuaihj/article/details/14223015
- 王朝 第五周
- 串口通信USART接收程序