NOIP 模拟 7 考试总结
T1
超级大水题,用 \(kmp\) 和 \(hash\) 均能过,但都忘了,结果只打了个暴力。难受。板子题,题解就不放了
Code
#include<bits/stdc++.h>
#define ri register int
#define p(i) ++i
using namespace std;
namespace IO{
char buf[1<<21],*p1=buf,*p2=buf;
#define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++
inline int read() {
ri x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return x*f;
}
}
using IO::read;
namespace nanfeng{
#define cmax(x,y) ((x)>(y)?(x):(y))
#define cmin(x,y) ((x)>(y)?(y):(x))
#define FI FILE *IN
#define FO FILE *OUT
typedef unsigned long long ull;
static const int N=1e5+7;
static const ull P=131;
char a[N<<1],b[N];
int st[N],cnt,T,la,lb,ans;
ull ah[N],bh[N],p[N];
inline int check(int x) {
if (ah[x]==bh[lb]-bh[lb-x]*p[x]) return 1;
return 0;
}
inline void init() {cnt=0;ans=0;}
inline int main() {
// FI=freopen("nanfeng.in","r",stdin);
// FO=freopen("nanfeng.out","w",stdout);
T=read();
p[0]=1;
for (ri i(1);i<=N-5;p(i)) p[i]=p[i-1]*P;
for (ri z(1);z<=T;p(z)) {
init();
la=read(),lb=read();
scanf("%s",a+1);
scanf("%s",b+lb+1);
for (ri i(1);i<=lb;p(i)) {
if (i<=la&&a[i]==b[lb+1]) st[p(cnt)]=i;
b[i]=a[i];
bh[i]=ah[i]=ah[i-1]*P+(ull)(a[i]);
}
lb+=1;
if (lb<=la&&a[lb]==b[lb]) {printf("%d\n",ans=lb);continue;}
bh[lb]=bh[lb-1]*P+(ull)(b[lb]);
for (ri i(cnt);i;--i) if (check(st[i])) {ans=st[i];break;}
printf("%d\n",ans);
}
return 0;
}
}
int main() {return nanfeng::main();}
T2
一道 \(tarjan\) 缩点的题,结果光求了割点,没缩,还乐呵了半天
T3
一道很好的利用单调性的题,可以通过维护一个单调指针,答案递推而得