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

 

 

 

 

 

 

 


目录
相关文章
|
机器学习/深度学习 算法 搜索推荐
精确率(Precision)和召回率(Recall)
精确率(Precision)和召回率(Recall)是用于评估分类模型性能的指标。它们通常用于二分类问题,例如判断一个样本是正例(Positive)还是负例(Negative)。
7286 0
|
Linux iOS开发 MacOS
pnpm全局安装报错:Run “pnpm setup“ to create it automatically, or set the global-bin-dir setting, or the PN
pnpm全局安装报错:Run “pnpm setup“ to create it automatically, or set the global-bin-dir setting, or the PN
3434 0
|
算法 Java
JVM垃圾回收算法,解析新生代为什么要有两个survivor区域
复制算法将可用内存按容量划分为大小相等的两块,每次只使用其中的一块。当这一块的内存用完了,就将还存活着的对象复制到另外一块上面,然后再把已使用过的内存空间一次清理掉。
333 0
|
搜索推荐 SEO
如何增加网站流量 怎么快速增加网站流量的25种方法
如何提升网站流量?快速增加网站流量的25种方法 向营销人员或企业主询问他们在世界上最喜欢什么,他们可能会告诉你“更多客户”。经常出现在商家愿望清单上的顾客之后?他们网站的流量更多。有很多方法可以增加您网站的流量,在今天的帖子中,我们将查看其中的25个,包括几种免费提升网站流量的方法。
3874 0
|
2天前
|
人工智能 运维 安全
|
4天前
|
SpringCloudAlibaba 负载均衡 Dubbo
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
本文对比分析了SpringCloudAlibaba框架下Feign与Dubbo的服务调用性能及差异。Feign基于HTTP协议,使用简单,适合轻量级微服务架构;Dubbo采用RPC通信,性能更优,支持丰富的服务治理功能。通过实际测试,Dubbo在调用性能、负载均衡和服务发现方面表现更出色。两者各有适用场景,可根据项目需求灵活选择。
384 124
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
|
7天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
682 107
|
1天前
|
算法 Python
【轴承故障诊断】一种用于轴承故障诊断的稀疏贝叶斯学习(SBL),两种群稀疏学习算法来提取故障脉冲,第一种仅利用故障脉冲的群稀疏性,第二种则利用故障脉冲的额外周期性行为(Matlab代码实现)
【轴承故障诊断】一种用于轴承故障诊断的稀疏贝叶斯学习(SBL),两种群稀疏学习算法来提取故障脉冲,第一种仅利用故障脉冲的群稀疏性,第二种则利用故障脉冲的额外周期性行为(Matlab代码实现)
221 152