T73,求教
查看原帖
T73,求教
60932
Linno楼主2022/5/5 02:29

rt,不懂为什么T。

#pragma GCC optimize("Ofast", "inline", "-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
//#define int long long
using namespace std;
const int N=1e5+7,base=131;
const int mod=1e9+7;
typedef unsigned long long ull;

//int read(){	int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') f=f*-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
//void write(int x){if(x>9) write(x/10);putchar(x%10+'0');}

string S,str[N],tmp[N];
int n,f[N],pre[N],len;
ull sum[N],bb[N],hs[N];

unordered_map<ull,int>mp;

inline ull get(int i,int l){ //从i开始到i+l位的哈希值 
	return sum[i+l-1]-sum[i-1]*bb[l];
}

inline ull get_hs(string s,int L){  //得到整个字符串的哈希值 
	ull res=0;
	for(int i=0;i<L;i++){
		res=res*base+s[i]-'a'+1;
	}
	return res;
}

inline void check(string &s,int L){
	for(int i=0;i<L;i++){
		if(s[i]>='A'&&s[i]<='Z') s[i]+=32;
	}
}

void Solve(){
	cin>>len;
	cin>>S;
//	for(int i=1;i<=len;i++){S+='A'+rand()%26;}
	reverse(S.begin(),S.end());
	check(S,len);
	bb[0]=1;sum[0]=0;
	for(int i=1;i<=len;i++){
		bb[i]=bb[i-1]*base;
		sum[i]=(sum[i-1]*base+S[i-1]-'a'+1);
	}
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>str[i];
		int L=str[i].length();
		tmp[i]=str[i]; 
		check(str[i],L);
		hs[i]=get_hs(str[i],L); //得到所有子串的哈希值 
		mp[hs[i]]=i;
	}
	f[0]=1;
	int L;
	for(int i=1;i<=len;i++){ //前i位是否能拼出 
		for(int j=max(0,i-1000);j<=i;j++){ //寻找j到i的子串 
			L=i-j;
			if(f[j]&&mp[get(j+1,L)]){
				f[i]=mp[get(j+1,L)];
				pre[i]=j;
				break;
			}
		}
	}
	int j=len;
	while(j) cout<<tmp[f[j]]<<" ",j=pre[j];
}

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	int T=1;
	//cin>>T;
	while(T--){
		Solve();
	}
	return 0;
}
2022/5/5 02:29
加载中...