CF#215DIV2:C. Sereja and Algorithm
来源:互联网 发布:玩别人老婆知乎 编辑:程序博客网 时间:2024/05/20 02:27
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"的子串则输出NO,否则YES,小于3的直接输出YES
思路:因为会超时,所以一开始就用哈希记录相应位置x,y,z的数量,然后再每次属入区间时根据哈希计算即可
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;char str[100005];int hash_x[100005],hash_y[100005],hash_z[100005];int len;int abs(int a){ return a<0?-a:a;}int main(){ int i,j,x,y,z; while(~scanf("%s",str)) { len = strlen(str); x = 0; y = 0; z = 0; memset(hash_x,0,sizeof(hash_x)); memset(hash_y,0,sizeof(hash_y)); memset(hash_z,0,sizeof(hash_z)); for(i = 0;i<len;i++) { if(str[i] == 'x') x++; else if(str[i] == 'y') y++; else z++; hash_x[i] = x; hash_y[i] = y; hash_z[i] = z; } int m,l,r; scanf("%d",&m); while(m--) { scanf("%d%d",&l,&r); if(r-l+1<3) printf("YES\n"); else { x = hash_x[r-1]-hash_x[l-2]; y = hash_y[r-1]-hash_y[l-2]; z = hash_z[r-1]-hash_z[l-2]; if(abs(x-y) <= 1 && abs(x-z)<=1 && abs(y-z)<=1) printf("YES\n"); else printf("NO\n"); } } } return 0;}
- CF#215DIV2:C. Sereja and Algorithm
- CF-Sereja and Algorithm
- CF#215DIV2:B. Sereja and Suffixes
- CF#215 DIV2: B. Sereja and Suffixes
- CF#215DIV2:A. Sereja and Coat Rack
- Codeforces Round #215 (diy.2) C. Sereja and Algorithm
- CodeForces #230 div2 C Sereja and Prefixes
- CF#215DIV2:B. Sereja and Suffixes(哈希记录)
- Codeforces 368C Sereja and Algorithm【思维】
- CF 426C--Sereja and Swaps
- Codeforces Round #215_div2_C. Sereja and Algorithm
- Sereja and Algorithm
- codeforces Sereja and Algorithm
- C. Sereja and Algorithm codeforces-problem-368C
- CF#196:DIV2:C-Xenia and Weights
- CF 263div2 C. Appleman and Toastman
- CF 290Div2 C Fox And Names
- cf 291div2 C. Watto and Mechanism
- msys 编译 openresty 记录(失败)
- linux学习笔记--鸟哥的linux私房菜第0章 计算机概论
- CF#215DIV2:A. Sereja and Coat Rack
- protel的一些记录
- CF#215DIV2:B. Sereja and Suffixes
- CF#215DIV2:C. Sereja and Algorithm
- codeforces Round #215 div2 题解
- CodeForce Round 215 DIV2 C 使用了线段树 但是多此一举...
- Codeforces Round #215 (Div. 1)
- 六百链网络收藏夹 博客正式开通!
- 黑马程序员_HashCode_框架_配置文件
- Django admin中使用Django-tinymce富文本编辑框
- Java同步问题
- asp获取ip地址的代码