A题读透题意就是查询区间x,y,z的个数,如果区间长度不超过2或者x,y,z个数的最大值与最小值的差不超过了就是YES,否则就是NO因为如果最大的差值小于等于2的话,无论如何都可以构造出一个序列使得每三个字符符合特定要求...
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;
typedef long long ll;
int x[110000],y[110000],z[110000];
char s[110000];
int n,m,p,q;
int main()
{
// freopen("in.txt","r",stdin);
scanf("%s",&s);
int len=strlen(s);
getchar();
memset(x,0,sizeof x);
memset(y,0,sizeof y);
memset(z,0,sizeof z);
for (int i=0; i<len; i++)
{
if (s[i]=='x') x[i+1]=1;
if (s[i]=='y') y[i+1]=1;
if (s[i]=='z') z[i+1]=1;
}
for (int i=1; i<=len; i++)
{
x[i]+=x[i-1];
y[i]+=y[i-1];
z[i]+=z[i-1];
}
int xx,yy,zz;
int l,r;
scanf("%d",&m);
int maxx,minn;
for (int i=1; i<=m; i++)
{
scanf("%d%d",&l,&r);
if (r-l+1<=2)
{
puts("YES");
continue;
}
xx=x[r]-x[l-1];
yy=y[r]-y[l-1];
zz=z[r]-z[l-1];
maxx=max(xx,max(yy,zz));
minn=min(xx,min(yy,zz));
if (maxx-minn>=2) puts("NO");
else puts("YES");
}
return 0;
}
B也是个水题,先hash一下,然后用count[k]记录串b中每个数(k)出现的次数,然后用两个指针在a串里跑一遍,如果跑到某个位置发现count[a[i]]==0了,头指针后移并且恢复头指针的数据,头尾相差恰好为m的时候头指针的位置就是一个解..O(n)可以实现...要注意的是每次跑到尾以后,要让头指针也跑到尾来恢复数据,每次暴力用memcpy恢复的话真的会跪掉的- -.....
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
int ct[330000],ct2[330000];
int n,m,p,q,k;
int a[330000],b[330000];
int tmp[330000];
int cnt;
int main()
{
// freopen("in.txt","r",stdin);
scanf("%d%d%d",&n,&m,&q);
cnt=0;
for (int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
tmp[cnt++]=a[i];
}
map<int,int>hash;
sort(tmp,tmp+n);
cnt=unique(tmp,tmp+n)-tmp;
for (int i=0; i<cnt; i++)
hash[tmp[i]]=i;
for (int i=1; i<=n; i++)
a[i]=hash[a[i]];
for (int i=1; i<=m; i++)
scanf("%d",&b[i]);
memset(ct,0,sizeof ct);
for (int i=1; i<=m; i++)
{
if (hash.find(b[i])==hash.end())
{
puts("0");
return 0;
}
b[i]=hash[b[i]];
ct[b[i]]++;
}
vector<int> ans;
for (int i=1; i<=q; i++)
{
int st=i,ed=i;
while (ct[a[st]]==0 && st<=n) st+=q;
// ct[a[st]]--;
ed=st;
int len=0;
while (ed<=n)
{
if (ct[a[ed]])
{
ct[a[ed]]--;
ed+=q;
len++;
if (len==m) ans.push_back(st);
}
else
{
if (st<ed)
{
ct[a[st]]++;
st+=q;
len--;
}
else
{
while (ct[a[st]]==0 && st<=n) st+=q;
// ct[a[st]]--;
ed=st;
len=0;
}
}
}
while (st<ed && st<=n)
{
ct[a[st]]++;
st+=q;
len--;
}
}
printf("%d\n",ans.size());
for (int i=0; i<ans.size(); i++) tmp[i]=ans[i];
sort(tmp,tmp+ans.size());
if (ans.size()>0)
{
printf("%d",tmp[0]);
for (int i=1; i<ans.size(); i++)
printf(" %d",tmp[i]);
}
return 0;
}