A. Sereja and Algorithm----思维+贪心
来源:互联网 发布:人工智能柴玉梅版答案 编辑:程序博客网 时间:2024/05/16 14:30
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.
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).
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.
题目链接:http://codeforces.com/contest/367/problem/A
神奇的题目,在题意没有完全明确下蒙着过了...
题目的意思是说输入一个字符串,当字符串的长度不够3的时候输出YES,当字符串排列之后可以使得每三个连续的字符是"zyx","yxz","xzy"的时候输出YES,其他情况输出NO。
我们可以构造一个最长串"zyxzyxz",最长串的构造过程是我们先写一个"zyx",然后在后面加一个z,发现不平衡了,然后加一个y,还不平衡,再加一个x。此过程中我们发现xyz的数量始终差不超过1,然后添加就会平衡。
代码:
#include <cstdio>#include <cstring>#include <iostream>#include <cmath>#include <algorithm>using namespace std;const int maxn=300000;char s[maxn];int sumx[maxn];int sumy[maxn];int sumz[maxn];void Init(int len){ sumx[0]=sumy[0]=sumz[0]=0; for(int i=1;i<=len;i++){ if(s[i]=='x'){ sumx[i]=sumx[i-1]+1; sumy[i]=sumy[i-1]; sumz[i]=sumz[i-1]; } if(s[i]=='y'){ sumy[i]=sumy[i-1]+1; sumx[i]=sumx[i-1]; sumz[i]=sumz[i-1]; } if(s[i]=='z'){ sumz[i]=sumz[i-1]+1; sumy[i]=sumy[i-1]; sumx[i]=sumx[i-1]; } }}int main(){ scanf("%s",s+1); int len=strlen(s+1); Init(len); int m; scanf("%d",&m); for(int i=1;i<=m;i++){ int a,b; scanf("%d%d",&a,&b); if(b-a+1<3){ printf("YES\n"); continue; } int k=sumx[b]-sumx[a-1]; int kk=sumy[b]-sumy[a-1]; int kkk=sumz[b]-sumz[a-1]; if(k>0&&kk>0&&kkk>0&&abs(k-kkk)<=1&&abs(k-kk)<=1&&abs(kkk-kk)<=1){ printf("YES\n"); } else{ printf("NO\n"); } } return 0;}
- A. Sereja and Algorithm----思维+贪心
- codeforces --A - Sereja and Algorithm
- Codeforces 368C 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(思维)
- CF-Sereja and Algorithm
- 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 425A Sereja and Swaps(暴力+贪心)
- CodeForces A. Sereja and Swaps(暴力+贪心啊)
- A. Sereja and Bottles
- A. Sereja and Dima
- KVC ,NSKeyValueCoding 浅谈
- css 的一些属性使用
- redis
- 关于Java中方法重写的访问修饰符和返回值类型的限制
- 关于电源做了倒灌处理,以更快的泄放电压的例子
- A. Sereja and Algorithm----思维+贪心
- 数据结构课设(散列表的设计与实现)
- 附加的XML和正则表达式
- 最大堆、索引堆、二叉搜索树的JavaScript实现
- jquery实现移动端数据列表返回原来位置
- IDEA jetty
- 公式
- 人生本过客,何必千千结
- Spring MVC 学习总结(三)——请求处理方法Action详解