感觉复杂度没问题,为什么会TLE啊?
查看原帖
感觉复杂度没问题,为什么会TLE啊?
538521
Konjac_16楼主2022/6/9 19:19
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mid ((l+r)>>1)
// #define mod (1000000007)
#define mk make_pair
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define rep(i,a,b) for(register int i=(a);i>=(b);--i)
inline namespace IO{
	inline int read(){
		int x=0;int f=1;char ch;
		while((ch=getchar())<'0'||x>'9')if(ch=='-')f=-1;
		while(ch>='0'&&ch<='9'){x=((x<<1)+(x<<3)+(ch^48)),ch=getchar();}
		return x*f;
	}
	void write(char x){putchar(x);}
	void write(const char *x){for(;*x;++x)putchar(*x);}
	void write(char *x){for(;*x;++x)putchar(*x);}
	void write(signed x){if(x<0)putchar('-'),x=-x;if(x>9)write(x/10);putchar('0'+x-x/10*10);}
	void write(long long x){
		if(x<0)putchar('-'),x=-x;
		if(x>9)write(x/10);
		putchar('0'+x-x/10*10);
	}
	void write(unsigned long long x){
		if(x>9)write(x/10);
		putchar('0'+x-x/10*10);
	}
	void write(double x){printf("%0.2lf",x);}
	void write(string s){cout<<s;}
	template<typename type1,typename type2,typename ...typen>
	void write(type1 a1,type2 a2,typen ...an){
		write(a1);
		write(a2,an...);
	}
}using namespace IO;
inline int gcd(int x,int y){return y==0?x:gcd(y,x%y);}
inline int lcm(int x,int y){return x/gcd(x,y)*y;}
const int N=100005;
const int mod1=998244353,mod2=1e9+7;
const int p1=29,p2=31;
int n;
string s[6];
int len[6];
int L;
ull hash1[6][250005];
ull qpow1[250005];
void init(){
	qpow1[0]=1;
	For(i,1,n)
	For(j,1,len[i]){
		hash1[i][j]=(hash1[i][j-1]*p1+s[i][j]);
		// hash2[i][j]=(hash2[i][j-1]*p2+s[i][j]);
	}
	For(i,1,L){
		qpow1[i]=qpow1[i-1]*p1;
		// qpow2[i]=qpow2[i-1]*p2;
	}
}
inline ull get(int i,int l,int r){
	ull h1=(hash1[i][r]-hash1[i][l-1]*qpow1[r-l+1]);
	// ull h2=(hash2[i][r]-hash2[i][l-1]*qpow2[r-l+1]);
	return h1;
}
// set<pair<ull,ull>> S[3];
unordered_map<ull,ull> S;
inline int check(int x){
	S.clear();
	for(int l=1;l+x-1<=len[1];++l){
		ull tmp=get(1,l,l+x-1);
		S[tmp]=1;
	}
	For(i,1,len[n]-x+1){
		int tot=0;
		if(!S[get(n,i,i+x-1)]){tot=1;}
		if(tot==0)return 1;
	}
	return 0;
}
 
signed main()
{
	n=2;
	For(i,1,n){
		cin>>s[i];
		len[i]=s[i].size();
		L=max(L,len[i]);
		s[i]='-'+s[i];
	}
	init();
	// check(8);
	int now=0;
	for(int step=1<<20;step>=1;step>>=1)
	if(now+step<=L&&check(now+step))now+=step;
	write(now);
	return 0;
} 
2022/6/9 19:19
加载中...