跳到正文

个人记录

maker

wind_boy约 13 分钟
原文

这是 2025年11月10日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月17日
篇幅
约 13 分钟

版本

  1. v2当前
  2. v1

replace.cpp

C++
#include<bits/stdc++.h>
#define N 200005
#define S 5200005
#define P 13331
#define mod 1000000027
#define B 50
using namespace std;
struct Trie{
    int trans[S][26];
    int tot;
    int root[N];
    vector<int> ed[S];
    inline void insert(int Rt,char *s,int L,int R,int id,bool o){
        if(!root[Rt]) root[Rt]=++tot;
        int now=root[Rt];
        if(o)for(int i=L;i<=R;i++){
            if(!trans[now][s[i]-'a']) trans[now][s[i]-'a']=++tot;
            now=trans[now][s[i]-'a'];
        }
        else for(int i=R;i>=L;i--){
            if(!trans[now][s[i]-'a']) trans[now][s[i]-'a']=++tot;
            now=trans[now][s[i]-'a'];
        }
        ed[now].push_back(id);
    }
    inline void query(int Rt,char *s,int L,int R,vector<int> &v,bool o){
        int now=root[Rt];
        for(auto nx:ed[now]) v.push_back(nx);
        if(o)for(int i=L;i<=R;i++){
            now=trans[now][s[i]-'a'];
            if(!now) break;
            for(auto nx:ed[now]) v.push_back(nx);
        }
        else for(int i=R;i>=L;i--){
            now=trans[now][s[i]-'a'];
            if(!now) break;
            for(auto nx:ed[now]) v.push_back(nx);
        }
    }
}fr,ed;
map<pair<int,int>,int> mp;
int cnt;
inline int grt(int h1,int h2){
    if(mp.count({h1,h2})) return mp[{h1,h2}];
    return mp[{h1,h2}]=++cnt;
}
int bs[S];
int hsh[S];
inline int geths(int L,int R){
    if(L>R) return 0;
    return (hsh[R]-1ll*hsh[L-1]*bs[R-L+1]%mod+mod)%mod;
}
inline int ghs(char *s,int l,int r){
    int hs=0;
    for(int i=l;i<=r;i++) hs=(1ll*hs*P+s[i])%mod;
    return hs;
}
char s[S],t[S];
struct DS{
    int col[N],val[N],now;
    int &operator[](int x){
        if(col[x]!=now) col[x]=now,val[x]=0;
        return val[x];
    }
    void clear(){
        now++;
    }
}ds;
map<pair<int,int>,int> hscnt[N];
signed main(){
	freopen("replace.in","r",stdin);
	freopen("replace.out","w",stdout);
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    bs[0]=1;
    for(int i=1;i<S;i++) bs[i]=1ll*bs[i-1]*P%mod;
    int n,q;
    cin>>n>>q;
    for(int i=1;i<=n;i++){
        cin>>(s+1)>>(t+1);
        int m=strlen(s+1);
        int L=0,R=m+1;
        while(L+1<=m and s[L+1]==t[L+1]) L++;
        while(R-1>=1 and s[R-1]==t[R-1]) R--;
        if(L>=R) continue;
        int h1=ghs(s,L+1,R-1),h2=ghs(t,L+1,R-1);
        int id=grt(h1,h2);
        if(L<=B and R>=m-B+1){
            hscnt[id][{ghs(s,1,L),ghs(s,R,m)}]++;
            continue;
        }
        fr.insert(id,s,1,L,i,0);
        ed.insert(id,t,R,m,i,1);
    }
    while(q--){
        cin>>(s+1)>>(t+1);
        int m=strlen(s+1);
        int L=0,R=m+1;
        while(L+1<=m and s[L+1]==t[L+1]) L++;
        while(R-1>=1 and s[R-1]==t[R-1]) R--;
        int sum=0;
        if((int)strlen(t+1)!=m){
            cout<<0<<'\n';
            continue;
        }
        int h1=ghs(s,L+1,R-1),h2=ghs(t,L+1,R-1);
        if(!mp.count({h1,h2})){
            cout<<0<<'\n';
            continue;
        }
        int id=grt(h1,h2);
        for(int i=1;i<=m;i++) hsh[i]=(1ll*hsh[i-1]*P+s[i])%mod;
        for(int i=max(1,L-B+1);i<=L+1;i++){
            for(int j=R-1;j<=min(R+B-1,m);j++){
                sum+=hscnt[id][{geths(i,L),geths(R,j)}];
            }
        }
        if(m<=B){
            cout<<sum<<'\n';
            continue;
        }
        vector<int> pre,suf;
        fr.query(id,s,1,L,pre,0);
        ed.query(id,t,R,m,suf,1);
        ds.clear();
        for(auto nx:pre) ds[nx]++;
        for(auto nx:suf) sum+=ds[nx];
        cout<<sum<<'\n';
    }
    return 0;
}

sj.cpp

C++
#include<bits/stdc++.h>
#define fo(i,l,r) for(int i=(l);i<=(r);++i)
#define fd(i,l,r) for(int i=(l);i>=(r);--i)
#define fu(i,l,r) for(int i=(l);i<(r);++i)
#define pi pair<int,int>
#define eb emplace_back
#define vi vector<int>
#define fi first
#define se second
#define ll long long
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int N=2e5+7,M=5e6+7;
int n,q,o,a[N],b[N],V,tp,W;
int t[M];
void gt(int n,int *a,int m)
{
	fu(i,1,m) t[i]=i;
	shuffle(t+1,t+m,rnd);
	sort(t+1,t+n);
	t[n]=m;
	fo(i,1,n) a[i]=t[i]-t[i-1];
}
void calc(string &S,int n)
{
	S.resize(n);
	fu(i,0,n) S[i]=rnd()%(tp<=5?6:26)+'a';
	if(V)
	{
		fu(i,0,n) S[i]='a';
		S[rnd()%n]='b';
	}
}
int main(int argc,char* argv[])
{
	freopen("replace.in","w",stdout);
	string num=argv[1];
	fu(i,0,num.size()) tp=tp*10+num[i]-'0';
	if(1<=tp&&tp<=2) n=q=30,o=200;
	if(3<=tp&&tp<=5) n=q=300,o=2000;
	if(6<=tp&&tp<=6) n=q=1000,o=1e6;
	if(7<=tp&&tp<=8) n=q=10000,o=1e6;
	if(9<=tp&&tp<=10) n=q=1e5,o=1e6;
	if(11<=tp&&tp<=12) n=q=2e5,o=2e6;
	if(13<=tp&&tp<=14) n=q=2e5,o=5e6;
	if(15<=tp&&tp<=16) n=q=2e5,o=5e6;
	if(17<=tp&&tp<=20) n=q=2e5,o=5e6;
	if(6<=tp&&tp<=8) q=1;
	if(13<=tp&&tp<=14) q=1;
	if(tp==6||tp==9||tp==10||tp==15||tp==16) V=1;
	if(argc==3)//若运行 maker.bat (捆绑) 则会进入此部分
	{
		num=argv[2];
		fu(i,0,num.size()) W=W*10+num[i]-'0';
		n=max(n>>(W-1),1),q=max(q>>(W-1),1);
	}
	printf("%d %d\n",n,q);
	o>>=1;
	if(V) gt(n,a,o);
	else gt(n,a,o-n);
	if(V) gt(q,b,o);
	else gt(q,b,o-q);
	fo(i,1,n)
	{
		if(V) a[i]++;
		string S,T;
		while(S==T) calc(S,a[i]),calc(T,a[i]);
		fu(j,0,S.size()) putchar(S[j]); putchar(' ');
		fu(j,0,T.size()) putchar(T[j]); putchar('\n');
	}
	fo(i,1,q)
	{
		if(V) b[i]++;
		int l1=b[i],l2=b[i];
		if(b[i]>1&&rnd()%10000==0)
		{
			if(rnd()&1) l1--;
			else l2--;
		}
		string S,T;
		while(S==T) calc(S,l1),calc(T,l2);
		fu(j,0,S.size()) putchar(S[j]); putchar(' ');
		fu(j,0,T.size()) putchar(T[j]); putchar('\n');
	}
}

maker.bat (不捆绑)

bat
@echo off
set name=replace
for /L %%i in (1,1,20) do (
echo Data %name%%%i
sj.exe %%i
%name%.exe
copy %name%.in %name%%%i.in >nul
copy %name%.out %name%%%i.out >nul
)

maker.bat (捆绑)

bat
@echo off
set name=replace
for /L %%j in (1,1,20) do (
echo Data %name%%%j
for /L %%i in (1,1,5) do (
sj.exe %%j %%i 
%name%.exe
copy %name%.in %name%%%j_%%i.in >nul
copy %name%.out %name%%%j_%%i.out >nul
)
)

down.bat

bat
@echo off
set name=replace
for /L %%i in (1,1,20) do (
echo Data %name%%%i
sj.exe %%i
%name%.exe
copy %name%.in ex_%name%%%i.in >nul
copy %name%.out ex_%name%%%i.out >nul
)


sampletester.bat

bat
@echo off
set name=replace
for /L %%i in (1,1,20) do (
copy ex_%name%%%i.in %name%.in >nul
echo Data %name%%%i
%name%.exe
fc.exe %name%.out ex_%name%%%i.out
pause
)