CF-Sereja and Algorithm
来源:互联网 发布:网络交换机设置 编辑:程序博客网 时间:2024/06/05 15:35
Description
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.
Sample Input
zyxxxxxxyyz55 51 31 111 43 6
YESYESNOYESNO
解体思路:这道题的题意太难理解了.意思是说,给你一串字符,m次操作,每一次取一个[li,ri]的连续子序列,如果子序列长度小于3,输出YES,如果对于该子序列无论怎样排列,都可以找出3个连续字符,既不是"zyx",也不是 "xzy",也不是 "yxz".那么输出NO,其他情况输出YES。那么我们可以从中找到规律,分别计算[li,ri]区间内(长度大于等于3),x,y,z,的个数,最大个数-最小个数〉1,则输出NO,否则YES。
代码如下:
#include<stdio.h>#include<string.h>#include<algorithm>#define INF 0x3f3f3f3fusing namespace std;char s[100010];int num[3];int x[100010];int y[100010];int z[100010];int main(){int t,i,l,r,X,Y,Z;while(scanf("%s",s)!=EOF){ int L=strlen(s); X=Y=Z=0; for(i=0;i<L;i++){ if(s[i]=='x') { X++; } else if(s[i]=='y'){ Y++; } else{ Z++; } x[i]=X;y[i]=Y;z[i]=Z; }scanf("%d",&t);while(t--){scanf("%d%d",&l,&r); if(r-l+1<3){ printf("YES\n"); continue; } if(l==1){ X=x[r-1]; Y=y[r-1]; Z=z[r-1]; } else{ X=x[r-1]-x[l-2]; Y=y[r-1]-y[l-2]; Z=z[r-1]-z[l-2]; } int max=-1; int min=INF; if(X>max) max=X; if(Y>max) max=Y; if(Z>max) max=Z; if(X<min) min=X; if(Y<min) min=Y; if(Z<min) min=Z; if(max-min>1)printf("NO\n"); else printf("YES\n");}}return 0;}
- CF-Sereja and Algorithm
- CF#215DIV2:C. Sereja and Algorithm
- Sereja and Algorithm
- codeforces Sereja and Algorithm
- codeforces --A - Sereja and Algorithm
- (CF)B. Sereja and Contests
- CF Sereja and Periods1 【暴力】
- Codeforces Round #215_div2_C. Sereja and Algorithm
- 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 368C Sereja and Algorithm【思维】
- A. Sereja and Algorithm----思维+贪心
- CF 314B: Sereja and Periods
- CF#215DIV2:B. Sereja and Suffixes
- CF 380A Sereja and Prefixes(二分)
- 工具类:快速字典转模型(NSObject 分类/runtime 用法2:获取属性列表)
- Android 数据操作之SQLiteDatabase
- Android小练习:过渡动画,引导页,判断是否第一次启动App
- 51 node 1050循环数组最大子段和
- 在Frame画图应用
- CF-Sereja and Algorithm
- 在window中net user administrator /active:no的解决策略
- 基站定位(Google API)
- 初识EJB
- 【问题】eclipse创建app时多出appcompat_v7
- mysql乐观锁总结和实践
- 【java】java获取ip地址(读取QQWry.dat文件形式)
- 来段代码提提神-那些无法回避的知识点之map以对象为key,static的加载顺序。
- IT忍者神龟之基于CAS实现单点登录(SSO)之配置CAS服务端的数据库查询认证机制(一)