您的位置:首页 > 编程语言 > Go语言

Codeforces Round #215_div2_C. Sereja and Algorithm

2013-12-04 20:15 357 查看
转载注明出处 
http://blog.csdn.net/moedane


 

 

传送门 http://codeforces.com/contest/368/problem/C

 

题意

给出一个只包含’x’,’y’,’z’的字符串,给出查询区间l和r,问[l,r]内任选出一个长度为3的子串进行重新排列,问最终能否使得[l,r]内任意长度为3的子串都达成"zyx", "xzy", "yxz"这样的形式。

(虽然原题不是这样描述的……但大意是酱紫问题不大)

 

思路

由于可以任选长度为3的子串进行重排,那么只需要保证[l,r]内x,y,z分别出现的次数等于lenth或lenth-1即可,不必在意其初始位置。这里用三个前缀数组即可统计。

其中x,y,z出现的次数,必须要有lenth%3个是等于lenth/3 + 1的,要有(n - lenth%3)个是等于lenth/3的。只有这种情况才能达到要求。

最后要特判,lenth小于3的直接输出YES。

 

代码

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <cctype>
#define bug puts("here");

using namespace std;

typedef long long ll;

const int maxn = 100086;
const int mod = 1000000007;
const double PI = atan(1.0)*4.0;

char a[maxn];
int x[maxn],y[maxn],z[maxn];

int main()
{
scanf("%s",a);
int len = strlen(a);
int i;
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
memset(z,0,sizeof(z));
//做前缀数组
for(i=1;i<=len;i++)
x[i] = x[i-1] + (a[i-1] == 'x');
for(i=1;i<=len;i++)
y[i] = y[i-1] + (a[i-1] == 'y');
for(i=1;i<=len;i++)
z[i] = z[i-1] + (a[i-1] == 'z');
int m;
scanf("%d",&m);
for(i=0;i<m;i++)
{
int l,r;
scanf("%d%d",&l,&r);
int xx,yy,zz;
xx = x[r] - x[l-1];//统计当前区间内x,y,z出现的次数
yy = y[r] - y[l-1];
zz = z[r] - z[l-1];
int lenth = r - l + 1;
if(lenth < 3)
{
puts("YES");
continue;
}
int t1,t2;
t1 = t2 = 0;
if(xx == lenth/3) t1 ++;
else if(xx == lenth/3 + 1) t2++;
if(yy == lenth/3) t1 ++;
else if(yy == lenth/3 + 1) t2++;
if(zz == lenth/3) t1 ++;
else if(zz == lenth/3 + 1) t2++;
if(t2 == lenth%3 && t1 == 3 - t2) puts("YES");
else puts("NO");
}
return 0;
}


 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息