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

 

 

 

 

 

 

 


目录
相关文章
|
4月前
|
算法 搜索推荐 大数据
算法(Algorithm)
算法(Algorithm)
85 0
|
4月前
|
机器学习/深度学习 算法 程序员
C++ Algorithm 库 算法秘境探索(Algorithm Wonderland Exploration)
C++ Algorithm 库 算法秘境探索(Algorithm Wonderland Exploration)
182 1
|
11月前
|
C语言 容器
【Hello Algorithm】认识一些简单的递归
【Hello Algorithm】认识一些简单的递归
64 0
|
机器学习/深度学习 算法 TensorFlow
维特比算法(Viterbi algorithm)
维特比算法(Viterbi algorithm)是一种用于解码隐马尔可夫模型(Hidden Markov Model,HMM)的动态规划算法。它用于找到给定观测序列条件下的最有可能的隐藏状态序列。
423 1
|
2月前
|
传感器
Algorithm
【7月更文挑战第22天】
38 0
|
4月前
|
算法
Aho Corasick Algorithm
Aho Corasick Algorithm
39 0
|
11月前
|
算法
【Hello Algorithm】贪心算法
【Hello Algorithm】贪心算法
58 0
|
算法 搜索推荐 程序员
Euclidean algorithm
数论算法是研究整数及其性质的算法。数论算法在密码学、编码、计算机科学和其他领域中有广泛的应用。以下是数论算法的一些常见的算法以及它们的实现方法和示例代码:
79 1
|
11月前
|
数据采集 算法 数据可视化
Forward Algorithm
前向算法(Forward Algorithm)是自组织映射(Self-Organizing Maps,简称 SOM)算法中的一种计算方法。它通过计算输入数据与输出层神经元之间的相似性,来确定输入数据在输出层中的位置。前向算法的主要思想是寻找最优的权重向量,使得相似的输入数据在输出层中靠近彼此。前向算法包括以下步骤:
53 0
|
搜索推荐
algorithm常用函数
algorithm常用函数