Sereja and Algorithm

来源:互联网 发布:2016淘宝追加评论期限 编辑:程序博客网 时间:2024/06/05 19:45

E - Sereja and Algorithm
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

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:

  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... 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 liri(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.

Sample Input

Input
zyxxxxxxyyz55 51 31 111 43 6
Output
YESYESNOYESNO

Hint

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.



题目分析:

5组测试数据

5 5 是第五个开始第五个结束,也就是 x

1 3 是第一个开始第3个结束 ,也就是zyx

1 11是第一个开始第11个结束,也就是 zyxxxxxxyyz

1   4是第1个开始第4个结束,也就是zyxx

3  6 是第3个开始第6个结束,也就是xxxx

目的是:如果被截取出来的字符串找一下有没有 这三种zyx  xzy  yxz 子串, 如果有这些组合出现就重新排列再找,也就是只要有一种排列是找不出这三种zyx  xzy  yxz 子串

中任意一种的就输出NO,否则就是YES。但是如果字符数少于3个的都是输出YES

其实就是如果截取的字符串中  x    y     z各自的总个数    分别做差如果差有大于1那么肯定能通过重新排列出现一种排列是找不出这三种zyx  xzy  yxz 子串中任意一种的。不信你自己举些例子试一下。


#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>#define maxn 100010using namespace std;char str[maxn];int a[maxn],b[maxn],c[maxn];int len;void fun(){memset(a,0,sizeof(a));memset(b,0,sizeof(b));memset(c,0,sizeof(c));int x=0,y=0,z=0;for(int i=0;i<len;i++){if(str[i]=='z')++z;if(str[i]=='x')++x;if(str[i]=='y')++y;a[i+1]=x;b[i+1]=y;c[i+1]=z;}}int main(){while(cin>>str){int s,e,m;len=strlen(str);fun();cin>>m;while(m--){cin>>s>>e;if(e-s<2) printf("YES\n");else{int as,bs,cs;as=a[e]-a[s-1];bs=b[e]-b[s-1];cs=c[e]-c[s-1];//printf("%d %d %d \n",as,bs,cs);if(abs(as-bs)<=1&&abs(as-cs)<=1&&abs(bs-cs)<=1)printf("YES\n");else printf("NO\n");}}}return 0; } 




0 0
原创粉丝点击