Codeforces 368C:Sereja and Algorithm(规律)
2016-10-12 23:11
344 查看
C. Sereja and Algorithm
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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:
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.
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.
Examples
input
output
Note
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.
题目大意:给你一个字符串(仅含x、y、z),n次查询,每次查询给出左右区间(L,R),问这个字符串在这个区间内,字符任意排序后,这个区间内每个子串都是xyz和yzx和zxy中的任意一个,如果符合的话输出YES,不符合输出NO。且根据第一条件,当给的区间小于3时,输出YES。
解题思路:xyz、xyzx、xyzxy、xyzxyz。。。这几个都符合提议要求,发现出现次数最多的那个字母的个数-出现次数最少的字母个数<=1,用这个就能判断了。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[100010];
int numx[100010];
int numy[100010];
int numz[100010];
int main()
{
int n,l,r;
scanf("%s",s+1);
int size=strlen(s+1);
numx[0]=0;//num[i]代表第i个字符前x的个数
numy[0]=0;
numz[0]=0;
for(int i=1;i<=size;i++)
{
if(s[i]=='x')
{
numx[i]=numx[i-1]+1;
numy[i]=numy[i-1];
numz[i]=numz[i-1];
}
if(s[i]=='y')
{
numy[i]=numy[i-1]+1;
numx[i]=numx[i-1];
numz[i]=numz[i-1];
}
if(s[i]=='z')
{
numz[i]=numz[i-1]+1;
numy[i]=numy[i-1];
numx[i]=numx[i-1];
}
}
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&l,&r);
int qujian=r-l+1;
if(qujian<3)//第一条件
{
printf("YES\n");
}
else
{
int x=numx[r]-numx[l-1];
int y=numy[r]-numy[l-1];
int z=numz[r]-numz[l-1];
if(x==0||y==0||z==0)//这个区间内没有x或y或z ,那么肯定不行
{
printf("NO\n");
}
else
{
if(max(x,max(y,z))-min(x,min(y,z))>1)//这个区间内出现次数最多的跟最少的字母的个数差
{
printf("NO\n");
}
else
{
printf("YES\n");
}
}
}
}
return 0;
}
C. Sereja and Algorithm
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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:
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.
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.
Examples
input
zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6
output
YES YES NO YES NO
Note
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.
题目大意:给你一个字符串(仅含x、y、z),n次查询,每次查询给出左右区间(L,R),问这个字符串在这个区间内,字符任意排序后,这个区间内每个子串都是xyz和yzx和zxy中的任意一个,如果符合的话输出YES,不符合输出NO。且根据第一条件,当给的区间小于3时,输出YES。
解题思路:xyz、xyzx、xyzxy、xyzxyz。。。这几个都符合提议要求,发现出现次数最多的那个字母的个数-出现次数最少的字母个数<=1,用这个就能判断了。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[100010];
int numx[100010];
int numy[100010];
int numz[100010];
int main()
{
int n,l,r;
scanf("%s",s+1);
int size=strlen(s+1);
numx[0]=0;//num[i]代表第i个字符前x的个数
numy[0]=0;
numz[0]=0;
for(int i=1;i<=size;i++)
{
if(s[i]=='x')
{
numx[i]=numx[i-1]+1;
numy[i]=numy[i-1];
numz[i]=numz[i-1];
}
if(s[i]=='y')
{
numy[i]=numy[i-1]+1;
numx[i]=numx[i-1];
numz[i]=numz[i-1];
}
if(s[i]=='z')
{
numz[i]=numz[i-1]+1;
numy[i]=numy[i-1];
numx[i]=numx[i-1];
}
}
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&l,&r);
int qujian=r-l+1;
if(qujian<3)//第一条件
{
printf("YES\n");
}
else
{
int x=numx[r]-numx[l-1];
int y=numy[r]-numy[l-1];
int z=numz[r]-numz[l-1];
if(x==0||y==0||z==0)//这个区间内没有x或y或z ,那么肯定不行
{
printf("NO\n");
}
else
{
if(max(x,max(y,z))-min(x,min(y,z))>1)//这个区间内出现次数最多的跟最少的字母的个数差
{
printf("NO\n");
}
else
{
printf("YES\n");
}
}
}
}
return 0;
}
相关文章推荐
- Codeforces--368C--Sereja and Algorithm(规律)
- Codeforces--368C--Sereja and Algorithm(规律)
- CodeForces 368C Sereja and Algorithm(dp)
- CodeForces 367A-Sereja and Algorithm【规律】
- 【CodeForces】368C - Sereja and Algorithm(思维)
- codeforces --A - Sereja and Algorithm
- C. Sereja and Algorithm codeforces-problem-368C
- Codeforces 367A - Sereja and Algorithm(前缀和)
- Codeforces_367A_Sereja and Algorithm(统计)
- Codeforces_841_C Leha and Function(贪心+构造|规律)
- Codeforces-368B-Sereja and Suffixes
- Codeforces 471C MUH and House of Cards【找规律+二分】
- CF-Sereja and Algorithm
- codeforces 553B B. Kyoya and Permutation(找规律)
- 【CodeForces 339B】Red and Blue Balls 找规律
- CodeForces 380C Sereja and Brackets(扫描线+树状数组)
- codeforces - 877C - Slava and tanks 【找规律呀找规律】
- codeforces 167C Wizards and Numbers(找规律,博弈)
- Codeforces 380 C. Sereja and Brackets
- Codeforces 592B The Monster and the Squirrel 【规律题】