CodeForces 367A-Sereja and Algorithm【规律】
来源:互联网 发布:美术教程视频软件 编辑:程序博客网 时间:2024/05/22 06:26
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 asq = 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". Ifq 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 substringslisli + 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 exceed105. It is guaranteed that strings only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Nextm 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.
zyxxxxxxyyz55 51 31 111 43 6
YESYESNOYESNO
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.
题目大意就是:
有一个字符串给出区间,问这里边的字母从后向前或从前向后转换,能不能变成由任意三位都是 "zyx", "xzy", "yxz"的字串。比如1 4 zyxx最后x放前面就满足。
我们可以发现能成的都是x y z任意两个的数量相减的绝对值不超过一。
#include<stdio.h>#include<cmath>#include<string.h>char map[100010];int x[100010];int y[100010];int z[100010];int as(int uu){if(uu<0)return -1*uu;return uu;} int main(){scanf("%s",map);int ll=strlen(map);int i,j;int xx,yy,zz;zz=xx=yy=0;for(i=0;i<ll;i++){if(map[i]=='x'){xx++;x[i]=xx;y[i]=yy;z[i]=zz;}if(map[i]=='y'){yy++;x[i]=xx;y[i]=yy;z[i]=zz;}if(map[i]=='z'){zz++;x[i]=xx;y[i]=yy;z[i]=zz;}}int m;scanf("%d",&m);for(i=0;i<m;i++){int l,r;scanf("%d%d",&l,&r);if(r-l<2){printf("YES\n");continue;}l--,r--;l--;int x1,y1,z1;x1=x[r]-x[l];y1=y[r]-y[l];z1=z[r]-z[l];if(as(x1-y1)<=1&&as(y1-z1)<=1&&as(x1-z1)<=1){printf("YES\n");continue;}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
- CodeForces - 368C Sereja and Algorithm (找规律&模拟)
- Codeforces--368C--Sereja and Algorithm(规律)
- Codeforces--368C--Sereja and Algorithm(规律)
- Codeforces 368C: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【思维】
- A. Sereja and Algorithm----思维+贪心
- CF-Sereja and Algorithm
- Sereja and Algorithm
- mac 升级自带svn
- checkenc - 自动文本编码识别
- Java中Date插入数据库的一些问题总结(二)
- Variably modified array at file scope
- 桂林电子科技大学操作系统课程设计(二)
- CodeForces 367A-Sereja and Algorithm【规律】
- D - Harmonic Number——(LightOJ 1234)
- Linux笔记(44)——不可改变位权限
- CSS Gradient详解
- 隐马尔科夫学习(HMM)
- 【BZOJ1898】[Zjoi2005]Swamp 沼泽鳄鱼【DP】【矩阵乘法】
- 允许用户仅修改一次用户名
- Android获取图片的路径方法总结
- flaskweb遇到的问题总结(持续更新)