首页 > 其他分享 >CodeForces - 368C Sereja and Algorithm (找规律&模拟)

CodeForces - 368C Sereja and Algorithm (找规律&模拟)

时间:2023-04-19 15:32:57浏览次数:47  
标签:Algorithm algorithm Sereja CodeForces test YES dp string

CodeForces - 368C 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 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.

//题意:先输入一个字符串,再输入一个n(表示询问次数),接下来n行每行输入两个数(l,r),表示子字符串的左端点和右端点,对于每个子字符串判断它是否是合格的。

对于给定的一个字符串,每次对这个字符串进行重新排列,如果每次都能找出一个连续子串(包含三个字符)s,并且这个s不是"zyx", "xzy", "yxz",那么这个字符串是不合格的(因为会一直在重排、查找),则输出NO,否则,输出YES。

//思路:

用一个二维数组dp来存放x,y,z的个数,具体为:

dp[i][1]表示前i位中x字符的个数;

dp[i][2]表示前i位中y字符的个数;

dp[i][3]表示前i位中z字符的个数;

接着运用找到的规律就可以解题了:

1、当字符数少于3时,肯定是合格的字符串;

2、当字符数大于3时,判断三种字符中最多的个数与最少的个数的差是否小于2,是的话就是个合格的,否则就不是合格的字符串。

知道这些,这个题就可以解决了。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define ull unsigned long long
#define ll long long
#define IN __int64
#define N 100010
#define M 1000000007
using namespace std;
char s[N];
int dp[N][4];
int a[5];
int main()
{
	int t,n,i,j,k,m;
	while(scanf("%s",s)!=EOF)
	{
		int len=strlen(s);
		dp[0][1]=0;dp[0][2]=0;dp[0][3]=0;
		for(i=0;i<len;i++)
		{
			if(s[i]=='x')
			{
				dp[i+1][1]=dp[i][1]+1;
				dp[i+1][2]=dp[i][2];
				dp[i+1][3]=dp[i][3];
			}
			else if(s[i]=='y')
			{
				dp[i+1][1]=dp[i][1];
				dp[i+1][2]=dp[i][2]+1;
				dp[i+1][3]=dp[i][3];
			}
			else
			{
				dp[i+1][1]=dp[i][1];
				dp[i+1][2]=dp[i][2];
				dp[i+1][3]=dp[i][3]+1;
			}
		}
		scanf("%d",&n);
		while(n--)
		{
			int l,r;
			scanf("%d%d",&l,&r);
			l--;
			a[1]=dp[r][1]-dp[l][1];
			a[2]=dp[r][2]-dp[l][2];
			a[3]=dp[r][3]-dp[l][3];
			sort(a+1,a+4);
			int s=a[1]+a[2]+a[3];
			if(s<3)
				printf("YES\n");
			else
			{
				if(a[3]-a[1]>=2)
					printf("NO\n");
				else
					printf("YES\n");
			}
		}
	}
	return 0;
}


标签:Algorithm,algorithm,Sereja,CodeForces,test,YES,dp,string
From: https://blog.51cto.com/u_16079508/6206442

相关文章

  • CodeForces - 616E Sum of Remainders (数论)大数取余求和 好题
    CodeForces-616ESumofRemaindersTimeLimit: 2000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uSubmit StatusDescriptionCalculatethevalueofthesum: nmod1 + nmod2 + nmod3 +...+ nmodm.Astheresultcanbeve......
  • Educational Codeforces Round 113 (Rated for Div. 2)
    题目链接B核心思路这个题目我觉得很好。首先分析下吧,如果有人需要执行操作二那么我们肯定就是给他们都打上平局是最优的。那么如果有人需要执行操作一呢,那么我们就可以把这些需要执行操作1的都搞一起。然后是他们成一个环。这样肯定就保证了每个人都会赢上一次。C核心思路......
  • Codeforces Round 866 (Div. 2)
    A.Yura'sNewName一个简单的dp,状态是\(f[i][0/1]\)表示前\(i\)位变成合法的且最后一位是^或_的最小代价。如果是_只能从^转移过来,如果是^则都可以转移过来#include<bits/stdc++.h>usingnamespacestd;voidsolve(){ strings; cin>>s; intn=s.size(); if(n=......
  • LeetCode:Search Algorithm
    LeetCode:SearchAlgorithm1\FirstuniquecharAlgorithmDesign利用字符数量的有限性,通过数组来映射(避免Hash_map的高复杂度)注意数组声明为intA[26]而不是charA[26];if(s=="")return''; intA[26]={0,0}; for(inti=0;i<s.length();i++){ A[s[i]-......
  • Codeforces Dp
    ZeroRemainderSum  采用辅助数组$ndp[m+1][\frac{m}{2}][k]$来求出每一行中在当前第$i$列,取了$j$个物品,总和模$k$的余数是$t$的最大和是多少。用$dp[n+1][k]$来转移每一行的状态。#include<bits/stdc++.h>usingnamespacestd;const......
  • Codeforces 1810G - The Maximum Prefix(DP)
    挺小清新的一道计数题。首先先分析下这个“最大前缀和”,按照最朴素的思路就是扫一遍每个前缀,然后记录一下当前的\(sum\)与前面的\(mx\),但是如果你一直陷在这个思路上你就似了,因为按照这个思路做,你DP状态里需要记录\(sum\)和\(mx\)两个维度,算上下标一维总共是\(n^3\),并......
  • Codeforces Round 625 (Div. 1, based on Technocup 2020 Final Round) A. Journey Pl
    https://codeforces.com/contest/1320/problem/AA.JourneyPlanning题目大意:给定一组数,问我们ai-aj==i-j的时候就可以把ai的值加起来,问我们可以凑到的最大总值是多少?input6107191015output26input1400000output400000input7892611122914out......
  • Codeforces Round 866 (Div. 2) ABC
    https://codeforces.com/contest/1820A.Yura'sNewName题目大意:给定一个字符串,每次这个表情^^或者这个表情^_^就是合法的问我们这个字符串至少要添加多少东西使得怎么看都是合法的?input7^______^___^_^^^_^___^^_^^_^^^^^_^_^^___^^_output5511032#......
  • Codeforces Round 832 (Div2)
    SwapGameAlice和Bob两个人在玩游戏。有一个长度为\(n\)的序列\(a\),Alice和Bob两人轮流完成一个操作,Alice先开始。每个人可以将数列的第一个数减\(1\),并将它与后面序列的一个数进行交换,如果一个人操作之前发现当前序列中的第一个数为\(0\),这个人就输了。问如果两......
  • Codeforces Round 764 (Div. 3) -- E. Masha-forgetful
    **题目大意:取去模板串中的子串可以组成一个给定的目标串,每个子串可以用无数次,输出组成的所需的串的信息题目中的取得子串必须“>=2”很好的提示了我们,想到一个式子2*x+3*y可以等于任何数,所以从之前的串都取长度为2,为3。在进行匹配。**structnode{ intl,......