QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#325304#7780. Dark LaTeX vs. Light LaTeXZhouShang#WA 11ms199368kbC++203.3kb2024-02-11 07:38:002024-02-11 07:38:01

Judging History

你现在查看的是最新测评结果

  • [2024-11-25 20:53:52]
  • hack成功,自动添加数据
  • (/hack/1258)
  • [2024-02-11 07:38:01]
  • 评测
  • 测评结果:WA
  • 用时:11ms
  • 内存:199368kb
  • [2024-02-11 07:38:00]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
int len[5005][5005],cnt[5005][5005];
string s,t;
typedef uint32_t ull;
typedef long long ll;
struct H{
	ull x; H(ull x=0) :x(x){}
	H operator+ (H o) {return x+o.x+(x+o.x<x);}
	H operator- (H o) {return *this + ~o.x;}
	H operator* (H o) {auto m=(__uint128_t)x*o.x;
		return H((ull)m)+(ull)(m>>64);
	}
	ull get() const{return x+!~x;}
	bool operator==(H o) const {return get()==o.get();}
	bool operator<(H o) const{return get()<o.get();}
};
static const H C = (ll)1e11+3;
struct HashInterval{
	vector<H> ha,pw;
	HashInterval(string& str):ha(str.size()+1),pw(ha){
		pw[0]=1;
		for(int i=0;i<str.size();i++){
			ha[i+1]=ha[i]*C+str[i],
			pw[i+1]=pw[i]*C;
		}
	}
	H hashInterval(int a,int b){
		return ha[b]-ha[a]*pw[b-a];
	}
};
vector <pair<int,int>> mp[1000007];
const int mod=1e6+7;
mt19937 r(2003);
int main(){
	int n,m;
	//cin>>n>>m;
	cin>>s>>t;
	//for(int i=0;i<5000;i++) s+=r()%26+'a';
	//for(int i=0;i<5000;i++) t+=r()%26+'a';
	n=s.size(),m=t.size();
	long long ans=0;
	int maxl=min(n,m)-1;
	for(int dif=1;dif<n;dif++)
		for(int j=n-1;j>=dif;j--){
			if(j==n-1) len[j-dif][j]=s[j-dif]==s[j];
			else len[j-dif][j]=s[j-dif]==s[j]?len[j-dif+1][j+1]+1:0;
			if(len[j-dif][j]) cnt[j-dif+1][j-1]++,cnt[j-dif+len[j-dif][j]+1][j-1]--;
			//cout<<j-dif<<" "<<j<<" "<<len[j-dif][j]<<'\n';
		}
	for(int i=1;i<n;i++)
		for(int j=1;j<n;j++)
			cnt[j][i]+=cnt[j-1][i];
	HashInterval S=HashInterval(s),T=HashInterval(t);
	for(int i=1;i<=maxl+1;i++){
		//unordered_map <ull,int> mp;
		for(int j=0;j<=m-i;j++){
			ull val=T.hashInterval(j,j+i).x;
			int r=val%mod,f=0;
			for(auto &t:mp[r]){
				if(t.first==val){
					f=1;
					t.second++;
					break;
				}
			}
			if(!f) mp[r].push_back({val,1});
		}
		for(int j=0;j<=n-i;j++){
			ull val=S.hashInterval(j,j+i).x;
			int r=val%mod,f=0;
			for(auto &t:mp[r]){
				if(t.first==val){
					f=t.second;
					break;
				}
			}
			ans+=1LL*(cnt[j][j+i-1]+1)*f;
			//cout<<j<<" "<<j+i-1<<" "<<S.hashInterval(j,j+i).x<<'\n';
		}
		for(int j=0;j<=m-i;j++){
			ull val=T.hashInterval(j,j+i).x;
			int r=val%mod,f=0;
			mp[r].clear();
		}
	}
	memset(cnt,0,sizeof(cnt)),memset(len,0,sizeof(len));
	swap(s,t),swap(n,m);
	for(int dif=1;dif<n;dif++)
		for(int j=n-1;j>=dif;j--){
			if(j==n-1) len[j-dif][j]=s[j-dif]==s[j];
			else len[j-dif][j]=s[j-dif]==s[j]?len[j-dif+1][j+1]+1:0;
			if(len[j-dif][j]) cnt[j-dif+1][j-1]++,cnt[j-dif+len[j-dif][j]+1][j-1]--;
		}
	for(int i=1;i<n;i++)
		for(int j=1;j<n;j++)
			cnt[j][i]+=cnt[j-1][i];
	S=HashInterval(s),T=HashInterval(t);
	for(int i=1;i<=maxl;i++){
		for(int j=0;j<=m-i;j++){
			ull val=T.hashInterval(j,j+i).x;
			int r=val%mod,f=0;
			for(auto &t:mp[r]){
				if(t.first==val){
					f=1;
					t.second++;
					break;
				}
			}
			if(!f) mp[r].push_back({val,1});
		}
		for(int j=0;j<=n-i;j++){
			ull val=S.hashInterval(j,j+i).x;
			int r=val%mod,f=0;
			for(auto &t:mp[r]){
				if(t.first==val){
					f=t.second;
					break;
				}
			}
			ans+=1LL*(cnt[j][j+i-1]+1)*f;
			//cout<<j<<" "<<j+i-1<<" "<<S.hashInterval(j,j+i).x<<'\n';
		}
		for(int j=0;j<=m-i;j++){
			ull val=T.hashInterval(j,j+i).x;
			int r=val%mod,f=0;
			mp[r].clear();
		}
	}
	cout<<ans<<'\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 11ms
memory: 199368kb

input:

abab
ab

output:

11

result:

wrong answer 1st numbers differ - expected: '8', found: '11'