时间不够,只能发程序了
A:
/*
1.在字符数组前面插个空: scanf("%s".chr+1);
注:这个空不能为0,最好附一个值,因为strlen是根据0的位置确定字符数组长度
2.在不能空一个位置的时候,可以选择把另一个数组的下标全部+1,数值不变。
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std ;
int next[10005];
char W[10005],T[1000005];
int main()
{
int n;
cin>>n;
for (int N=0;N<n;N++)
{
scanf("%s",W);
scanf("%s",T);
int lnW=strlen(W);
int lnT=strlen(T);
int ans=0,k;
memset(next,0,sizeof 0);
next[0]=next[1]=-1;
k=-1;
for (int i=1;i<lnW;i++)
{
while(k>-1&&W[i]!=W[k+1]) k=next[k+1];
if (W[i]==W[k+1]) k++;
next[i+1]=k;
}
k=-1;
for (int i=0;i<lnT;i++)
{
while(k>-1&&T[i]!=W[k+1]) k=next[k+1];
if (T[i]==W[k+1]) k++;
if (k==lnW-1)
{
k=next[k+1];
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}
B:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std ;
int p[220005];
int main()
{
char str[110005],now[110005];
while ((scanf("%s",str))!=EOF)
{
int size=0;
size=strlen(str);
now[0]='!';
now[1]='#';
int x=1;
for (int i=0;i<size;i++)
{
now[++x]=str[i];
now[++x]='#';
}
// printf("%s\n",now);
memset(p,0,sizeof p);
int bj=0;
int id=0;
size=x;
for (int i=1;i<=size;i++)
{
if (bj>i)
if (p[2*id-i]<bj-i)
p[i]=p[2*id-i];
else p[i]=bj-i;
else p[i]=1;
while(now[i-p[i]]==now[i+p[i]]) p[i]++;
if (i+p[i]>bj)
{
bj=i+p[i];
id=i;
}
}
int max_=0,ii;
for (int i=1;i<size;i++)
if (p[i]>max_)
{
max_=p[i];
ii=i;
}
max_--;
int s=ii-max_;
int e=ii+max_;
int ans=0;
for (int i=s;i<=e;i++)
if (now[i]!='#') ans++;
printf("%d\n",ans);
}
return 0;
}
Price List
#include<cstdio>
#include<algorithm>
using namespace std ;
int main()
{
long long a,sum=0;
int t,n,m;
scanf("%d",&t);
for (int T=0;T<t;T++)
{
sum=0;
scanf("%d%d",&n,&m);
for (int i=0;i<n;i++)
{
scanf("%I64d",&a);
sum+=a;
}
for (int i=0;i<m;i++)
{
scanf("%I64d",&a);
if (a<=sum) printf("0");
else printf("1");
}
printf("\n");
}
return 0;
}
NanoApe Loves Sequence
#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std ;
int a[1000000];
int b[1000000];
int main()
{
int t;
scanf("%d",&t);
for (int T=0;T<t;T++)
{
int n,max1=0,max2=0,max3=0;
int tot=0;
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
for (int i=2;i<=n;i++)
{
b[i]=abs(a[i]-a[i-1]);
if (b[i]>max1)
{
max3=max2;
max2=max1;
max1=b[i];
}
else if (b[i]>max2)
{
max3=max2;
max2=b[i];
}
else if (b[i]>max3)
{
max3=b[i];
}
}
for (int i=1;i<=n;i++)
{
if (i==1)
{
if (b[2]==max1) tot+=max2;
else tot+=max1;
}
else if (i==n)
{
if (b[n]==max1) tot+=max2;
else tot+=max1;
}
else
{
int now=abs(a[i-1]-a[i+1]);
if (b[i]==max1)
{
if (b[i+1]==max2) tot+=max(max3,now);
else tot+=max(max2,now);
}
else if (b[i+1]==max1)
{
if (b[i]==max2) tot+=max(max3,now);
else tot+=max(max2,now);
}
else tot+=max(max1,now);
}
}
printf("%d\n",tot);
}
return 0;
}
NanoApe Loves Sequence Ⅱ
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std ;
int a[200005],
b[200005];
bool use[200005];
int main()
{
int t,n,m,k;
scanf("%d",&t);
for (int T=0;T<t;T++)
{
int tot=0;
scanf("%d%d%d",&n,&m,&k);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
a[n+1]=m;
int flag=n+1;
for (int i=n;i>=1;i--)
{
b[i]=flag;
if (a[i]>m) flag=i;
}
b[n+1]=n+1;
int c=0;
bool now=false;
flag=1;
if (a[flag]>=m)
{
c++;
now=true;
}
while (c!=k)
{
flag=b[flag];
c++;
}
if (now) c--;
memset(use,true,sizeof use);
for (int i=1;i<=n;i++)
{
if (a[i]>=m&&use[i]) c++;
while (c<k)
{
c++;
use[flag]=false;
flag=b[flag];
use[flag]=false;
}
tot+=n-flag+1;
if (a[i]>=m) c--;
}
printf("%d\n",tot);
}
return 0;
}