CodeForces 367A-Sereja and Algorithm【规律】

我不是女神ヾ 2022-08-21 04:28 123阅读 0赞

A. 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… q**k. The algorithm consists of two steps:

  1. 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.
  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… s**n, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisl**i + 1… sri (1 ≤ l**i ≤ r**i ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (l**i, r**i) 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 l**i, r**i (1 ≤ l**i ≤ r**i ≤ 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

  1. zyxxxxxxyyz
  2. 5
  3. 5 5
  4. 1 3
  5. 1 11
  6. 1 4
  7. 3 6

Output

  1. YES
  2. YES
  3. NO
  4. YES
  5. 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.

题目大意就是:

有一个字符串给出区间,问这里边的字母从后向前或从前向后转换,能不能变成由任意三位都是 “zyx”, “xzy”, “yxz”的字串。比如1 4 zyxx最后x放前面就满足。

我们可以发现能成的都是x y z任意两个的数量相减的绝对值不超过一。

  1. #include<stdio.h>
  2. #include<cmath>
  3. #include<string.h>
  4. char map[100010];
  5. int x[100010];
  6. int y[100010];
  7. int z[100010];
  8. int as(int uu)
  9. {
  10. if(uu<0)
  11. return -1*uu;
  12. return uu;
  13. }
  14. int main()
  15. {
  16. scanf("%s",map);
  17. int ll=strlen(map);
  18. int i,j;
  19. int xx,yy,zz;
  20. zz=xx=yy=0;
  21. for(i=0;i<ll;i++)
  22. {
  23. if(map[i]=='x')
  24. {
  25. xx++;
  26. x[i]=xx;
  27. y[i]=yy;
  28. z[i]=zz;
  29. }
  30. if(map[i]=='y')
  31. {
  32. yy++;
  33. x[i]=xx;
  34. y[i]=yy;
  35. z[i]=zz;
  36. }
  37. if(map[i]=='z')
  38. {
  39. zz++;
  40. x[i]=xx;
  41. y[i]=yy;
  42. z[i]=zz;
  43. }
  44. }
  45. int m;
  46. scanf("%d",&m);
  47. for(i=0;i<m;i++)
  48. {
  49. int l,r;
  50. scanf("%d%d",&l,&r);
  51. if(r-l<2)
  52. {
  53. printf("YES\n");
  54. continue;
  55. }
  56. l--,r--;
  57. l--;
  58. int x1,y1,z1;
  59. x1=x[r]-x[l];
  60. y1=y[r]-y[l];
  61. z1=z[r]-z[l];
  62. if(as(x1-y1)<=1&&as(y1-z1)<=1&&as(x1-z1)<=1)
  63. {
  64. printf("YES\n");
  65. continue;
  66. }
  67. printf("NO\n");
  68. }
  69. return 0;
  70. }

发表评论

表情:
评论列表 (有 0 条评论,123人围观)

还没有评论,来说两句吧...

相关阅读