Codeforces 368C Sereja and Algorithm【思维】
来源:互联网 发布:linux编辑保存命令 编辑:程序博客网 时间:2024/06/05 14:57
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.
题目大意:
给你一个字符串,有m个询问,每次询问一个区间,我们可以将区间内所有字符重新排序,使得如果最终从中任意选择连续三个字符的子串都是"zyx", "xzy", "yxz"中的一种那么输出YES,否则输出NO。
如果长度小于3,明显输出YES、
思路:
因为这三个串:
zyx,xzy,yxz明显是根据挪一位字符的位子就可以互相得到的,那么很明显,如果我们的区间字符串的三种字符的个数如果最多出现的次数-最少出现的次数<=1,我们就能将其转换成目标串,也就是输出YES.
Ac代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;char a[100500];int x[100500];int y[100500];int z[100500];int main(){ while(~scanf("%s",a)) { int n=strlen(a); for(int i=0;i<n;i++) { if(i==0) { x[i]=0; y[i]=0; z[i]=0; } else { x[i]=x[i-1]; y[i]=y[i-1]; z[i]=z[i-1]; } if(a[i]=='x')x[i]++; if(a[i]=='y')y[i]++; if(a[i]=='z')z[i]++; } int q; scanf("%d",&q); while(q--) { int l,r; scanf("%d%d",&l,&r); if(r-l<=1)printf("YES\n"); else { int contx,conty,contz; r--; l--; if(l==0) { contx=x[r]; conty=y[r]; contz=z[r]; } else { contx=x[r]-x[l-1]; conty=y[r]-y[l-1]; contz=z[r]-z[l-1]; } int c[3]; c[0]=contx;c[1]=conty;c[2]=contz; sort(c,c+3); if(c[2]-c[0]<=1)printf("YES\n"); else printf("NO\n"); } } }}
- Codeforces 368C Sereja and Algorithm【思维】
- 【CodeForces】368C - Sereja and Algorithm(思维)
- C. Sereja and Algorithm codeforces-problem-368C
- CodeForces - 368C Sereja and Algorithm (找规律&模拟)
- Codeforces--368C--Sereja and Algorithm(规律)
- Codeforces--368C--Sereja and Algorithm(规律)
- Codeforces 368C:Sereja and Algorithm(规律)
- CodeForces 368C Sereja and Algorithm(dp)
- codeforces Sereja and Algorithm
- Codeforces 426C Sereja and Swaps【思维】
- Codeforces 315C Sereja and Contest【思维】
- codeforces --A - Sereja and Algorithm
- Codeforces Round #215 (diy.2) C. Sereja and Algorithm
- A. Sereja and Algorithm----思维+贪心
- Codeforces 381C Sereja and Brackets【二分+思维】好题~
- Codeforces Round #215_div2_C. Sereja and Algorithm
- Codeforces 367A Sereja and Algorithm
- CodeForces 367A-Sereja and Algorithm【规律】
- cat命令
- POJ3903(dp最长上升子序列STL)
- Glide使用详解
- jquery的样式操作,类操作(添加 删除 css)
- 数据结构实验之排序七:选课名单(卡内存的一道题。。坑)
- Codeforces 368C Sereja and Algorithm【思维】
- 接口
- 修改mysql数据库密码
- c/c++指针详解(二)----内存分配
- shell脚本的一些基础信息
- react学习笔记 item7 --- 组件的生命周期
- laravel学习之学生管理项目基础剖析
- 关于HTTPS的证书的那些事
- PLA算法python实现