Sereja and Algorithm

简介: Sereja and Algorithm


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

Sample Input

Input

zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6

Output

YES
YES
NO
YES
NO

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 100010
using 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;
 } 

 

 

 

 

 

 

 


目录
相关文章
|
8月前
|
算法 搜索推荐 大数据
算法(Algorithm)
算法(Algorithm)
110 0
|
8月前
|
机器学习/深度学习 算法 程序员
C++ Algorithm 库 算法秘境探索(Algorithm Wonderland Exploration)
C++ Algorithm 库 算法秘境探索(Algorithm Wonderland Exploration)
272 1
|
算法 安全 数据安全/隐私保护
密码学基础-对称密码算法(Symmetric-key Algorithm)
密码学基础-对称密码算法(Symmetric-key Algorithm)
|
C语言 容器
【Hello Algorithm】认识一些简单的递归
【Hello Algorithm】认识一些简单的递归
87 0
|
算法 C++
【Hello Algorithm】链表相关算法题
【Hello Algorithm】链表相关算法题
60 0
|
机器学习/深度学习 算法 TensorFlow
维特比算法(Viterbi algorithm)
维特比算法(Viterbi algorithm)是一种用于解码隐马尔可夫模型(Hidden Markov Model,HMM)的动态规划算法。它用于找到给定观测序列条件下的最有可能的隐藏状态序列。
546 1
|
6月前
|
传感器
Algorithm
【7月更文挑战第22天】
64 0
|
8月前
|
算法
Aho Corasick Algorithm
Aho Corasick Algorithm
65 0
|
算法 数据挖掘 Serverless
k-means Clustering Algorithm
k-均值聚类算法(k-means Clustering Algorithm)是一种将一组数据分成 k 个不同的簇的聚类算法。该算法基于距离作为相似性度量,即将数据对象划分为 k 个簇,使得每个簇中的数据对象之间的距离尽可能小,而不同簇之间的数据对象之间的距离尽可能大。
81 2
|
算法 搜索推荐 程序员
Euclidean algorithm
数论算法是研究整数及其性质的算法。数论算法在密码学、编码、计算机科学和其他领域中有广泛的应用。以下是数论算法的一些常见的算法以及它们的实现方法和示例代码:
101 1