http://codeforces.com/contest/368/problem/C
从左向右记录从1位置到每一个位置上x,y,z的个数。然后判断在l,r区间内的x,y,z的关系满不满足abs(x-y)<=1&&abs(x-z)<=1&&abs(y-z)<=1,满足输出YES,否则输出NO。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 200000 5 using namespace std; 6 7 char str[maxn]; 8 int hx[maxn],hy[maxn],hz[maxn]; 9 int m; 10 int l,r; 11 12 int main() 13 { 14 while(scanf("%s",str)!=EOF) 15 { 16 int t1=0,t2=0,t3=0; 17 memset(hx,0,sizeof(hx)); 18 memset(hy,0,sizeof(hy)); 19 memset(hz,0,sizeof(hz)); 20 int len=strlen(str); 21 for(int i=0; i<len; i++) 22 { 23 if(str[i]=='x') 24 { 25 t1++; 26 hx[i+1]=t1; 27 hy[i+1]=t2; 28 hz[i+1]=t3; 29 } 30 else if(str[i]=='y') 31 { 32 t2++; 33 hx[i+1]=t1; 34 hy[i+1]=t2; 35 hz[i+1]=t3; 36 } 37 else if(str[i]=='z') 38 { 39 t3++; 40 hx[i+1]=t1; 41 hy[i+1]=t2; 42 hz[i+1]=t3; 43 } 44 } 45 scanf("%d",&m); 46 for(int i=1; i<=m; i++) 47 { 48 scanf("%d%d",&l,&r); 49 int len=r-l+1; 50 if(len<3) 51 { 52 printf("YES "); 53 continue; 54 } 55 else 56 { 57 int x=hx[r]-hx[l-1]; 58 int y=hy[r]-hy[l-1]; 59 int z=hz[r]-hz[l-1]; 60 if(abs(x-y)<=1&&abs(x-z)<=1&&abs(y-z)<=1) 61 { 62 printf("YES "); 63 } 64 else printf("NO "); 65 } 66 } 67 } 68 return 0; 69 }