CodeForces 367A Sereja and Algorithm
2016-10-09 15:08
411 查看
C. Sereja and Algorithm
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
output
Note
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.
从左向右记录从1位置到每一个位置上x,y,z的个数。然后判断在l,r区间内的x,y,z的关系满不满足abs(x-y)<=1&&abs(x-z)<=1&&abs(y-z)<=1,满足输出YES,否则输出NO。
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6
output
YES YES NO YES NO
Note
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.
从左向右记录从1位置到每一个位置上x,y,z的个数。然后判断在l,r区间内的x,y,z的关系满不满足abs(x-y)<=1&&abs(x-z)<=1&&abs(y-z)<=1,满足输出YES,否则输出NO。
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; char s[110000]; int cnt_x[110000],cnt_y[110000],cnt_z[110000]; int main() { scanf("%s",s); int len = strlen(s); int x,y,z,i; x = y = z = 0; for(i = 0;i < len;i++) { cnt_x[i] = cnt_y[i] = cnt_z[i] = 0; if(s[i] == 'x') x++; else if(s[i] == 'y') y++; else if(s[i] == 'z') z++; cnt_x[i] = x; cnt_y[i] = y; cnt_z[i] = z; } int m,l,r; scanf("%d",&m); for(i = 0;i < m;i++) { scanf("%d%d",&l,&r); if(r - l + 1 < 3) { printf("YES\n"); continue; } x = cnt_x[r - 1] - cnt_x[l - 2]; y = cnt_y[r - 1] - cnt_y[l - 2]; z = cnt_z[r - 1] - cnt_z[l - 2]; if(abs(x - y) <= 1 && abs(z - y) <= 1 && abs(z - x) <= 1) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- codeforces B. 4-point polyline 解题报告
- Inna and New Matrix of Candies(CodeForces - 400B)
- 【打CF,学算法——三星级】CodeForces 567D One-Dimensional Battle Ships (二分)
- Codeforces 735B - Urbanization 贪心
- Codeforces 734E Anton and Tree【并查集缩点+树的直径】好题~
- codeforces 1A Theatre Square
- CodeForces 450-B. Jzzhu and Sequences
- CodeForces 405C
- CodeForces 681D Gifts by the List (树上DFS)
- 【Codeforces Beta Round #36】Codeforces 36E Two Paths
- CodeForces - 344A Magnets (模拟题)
- codeforces 814E An unavoidable detour for home
- codeforces——Little Pony and Sort by Shift
- Codeforces 489D Unbearable Controversy of Being
- 【打CF,学算法——三星级】CodeForces 513C Second price auction (进制状态表示)
- Codeforces 501D Misha and Permutations Summation
- Codeforces 626C Block Towers(二分)
- CodeForces - 767C Garland dfs(假剪枝)
- Codeforces 453C Little Pony and Summer Sun Celebration dfs树上构造
- codeforces-437C-The Child and Toy