Codeforce Round #215 Div2 C
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.
zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6
YES YES NO YES NO
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 #include <cstdio> 2 #include <iostream> 3 #include <vector> 4 #include <set> 5 #include <cstring> 6 #include <string> 7 #include <map> 8 #include <cmath> 9 #include <ctime> 10 #include <algorithm> 11 #include <queue> 12 13 using namespace std; 14 #define INF 0x7fffffff 15 #define maxm 1001 16 #define mp make_pair 17 #define pb push_back 18 #define rep(i,n) for(int i = 0; i < (n); i++) 19 #define re return 20 #define fi first 21 #define se second 22 #define sz(x) ((int) (x).size()) 23 #define all(x) (x).begin(), (x).end() 24 #define sqr(x) ((x) * (x)) 25 #define sqrt(x) sqrt(abs(x)) 26 #define y0 y3487465 27 //#define y1 y8687969 28 //#define fill(x,y) memset(x,y,sizeof(x)) 29 30 typedef vector<int> vi; 31 typedef long long ll; 32 typedef long double ld; 33 typedef double D; 34 typedef pair<int, int> ii; 35 typedef vector<ii> vii; 36 typedef vector<string> vs; 37 typedef vector<vi> vvi; 38 39 template<class T> T abs(T x) { re x > 0 ? x : -x; } 40 41 const int maxn = 110005; 42 43 int n, m, t, k; 44 int vis[maxn]; 45 int x[maxn], y[maxn], z[maxn]; 46 char s[maxn]; 47 int main(){ 48 scanf("%s", s); 49 n = strlen(s); 50 for (int i = 1; i <= n; i++){ 51 x[i] = x[i - 1]; y[i] = y[i - 1]; z[i] = z[i - 1]; 52 if (s[i-1] == 'x')x[i]++; 53 if (s[i-1] == 'y')y[i]++; 54 if (s[i-1] == 'z')z[i]++; 55 } 56 scanf("%d", &m); 57 while (m--){ 58 int a, b; 59 scanf("%d%d", &a, &b); 60 t = b - a + 1; 61 int t1=x[b]-x[a-1], t2=y[b]-y[a-1], t3=z[b]-z[a-1]; 62 int m1, m2, m3,flag=1; 63 m1 = max(max(t1, t2), t3); 64 m3 = min(min(t1, t2), t3); 65 m2 = t1 + t2 + t3 - m1 - m3; 66 if ((t - 1) / 3 + 1 < m1)flag = 0; 67 if ((t - 1-1) / 3 + 1 < m2)flag = 0; 68 if ((t - 1-1-1) / 3 + 1 < m3)flag = 0; 69 if (flag||t<=2)printf("YES\n"); 70 else printf("NO\n"); 71 } 72 return 0; 73 }
浙公网安备 33010602011771号