QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#672623#7780. Dark LaTeX vs. Light LaTeXKOH-#WA 16ms199432kbC++143.1kb2024-10-24 17:49:062024-10-24 17:49:07

Judging History

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

  • [2024-11-25 20:53:52]
  • hack成功,自动添加数据
  • (/hack/1258)
  • [2024-10-24 17:49:07]
  • 评测
  • 测评结果:WA
  • 用时:16ms
  • 内存:199432kb
  • [2024-10-24 17:49:06]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x){
	x=0;char ch=getchar();bool f=false;
	while(!isdigit(ch))	f|=ch=='-',ch=getchar();
	while(isdigit(ch))	x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	x=f?-x:x;return;
} 
template <typename T>
inline void print(T x){
	if(x<0)	putchar('-'),x=-x;
	if(x>9)	print(x/10);
	putchar(x%10^48);return; 
}
#define ll long long
#define ull unsigned long long 
const int N=5003;
char s[N],t[N];
char s1[N<<1];
int pi[N][N<<1];
const int BASE1=131,MOD1=1e9+7;
const ull BASE2=127;
int h1[N],p1[N];
ull h2[N],p2[N];
int len1,len2;

map<pair<int,ull>,int> rev;
pair<int,ull> Get(int l,int r){
//	cout<<l<<" "<<r<<" "<< (h1[r]-1ll*h1[l-1]*p1[r-l+1]%MOD1+MOD1)%MOD1<<" "<<h2[r]-h2[l-1]*p2[r-l+1]<<"??\n";
	return make_pair((h1[r]-1ll*h1[l-1]*p1[r-l+1]%MOD1+MOD1)%MOD1,h2[r]-h2[l-1]*p2[r-l+1]);
}
void Gethash1(){
	h1[0]=h2[0]=1,p1[0]=p2[0]=1;
	for(int i=1;i<=len1;++i){
		h1[i]=(1ll*h1[i-1]*BASE1+s[i]-'a'+1)%MOD1;
		h2[i]=(1ll*h2[i-1]*BASE2+s[i]-'a'+1);
		p1[i]=1ll*p1[i-1]*BASE1%MOD1;
		p2[i]=1ull*p2[i-1]*BASE2;
	}
}
void Gethash2(){
	h1[0]=h2[0]=1,p1[0]=p2[0]=1;;
	for(int i=1;i<=len2;++i){
		h1[i]=(1ll*h1[i-1]*BASE1+t[i]-'a'+1)%MOD1;
		h2[i]=(1ll*h2[i-1]*BASE2+t[i]-'a'+1);
		p1[i]=1ll*p1[i-1]*BASE1%MOD1;
		p2[i]=1ull*p2[i-1]*BASE2;
	}
//	cout<<h1[0]<<" "<<h1[1]<<" "<<h1[2]<<"!!!!!!!!!!!!!?\n"; 
}
int main(){
//	freopen("a.in","r",stdin);
	scanf("%s%s",s+1,t+1);
	len1=strlen(s+1),len2=strlen(t+1);
	Gethash2();
	for(int i=1;i<=len2;++i){
		for(int j=i;j<=len2;++j){
			pair<int,ull> tmp=Get(i,j);
			if(!rev.count(tmp))	rev[tmp]=1;
			else rev[tmp]++;
		}
	}
	for(int i=2;i<=len1;++i){
		for(int j=1;i+j-1<=len1;++j)	s1[j-1]=s[i+j-1];
		int now=len1-i+1;
		s1[now]='#';
	
		for(int j=now+1;j<=now+len1;++j)	s1[j]=s[j-now]; 
		for(int j=1;j<=now+len1;++j){
			int k=pi[i][j-1];
			while(k&&s1[k]!=s1[j])	k=pi[i][k-1];
			if(s1[k]==s1[j])	++k;
			pi[i][j]=k;
		}
		for(int j=now+1;j<=now+len1;++j)	pi[i][j-now]=pi[i][j];
	}
	ll ans=0;
	Gethash1(); 
	for(int i=1;i<=len1;++i){
		for(int j=i;j<=len1;++j){
//			cout<<i<<" "<<j<<" "<<rev[Get(i,j)]<<"???\n";
			ans+=1ll*rev[Get(i,j)]*(pi[j+1][i-1]+1);
		}
	}
//	cout<<ans<<"!!!!!!!!!!\n";
//	cout<<pi[3][2]<<"!!!!!!!!!!!\n"; 
	memset(pi,0,sizeof(pi));
	rev.clear();
	reverse(s+1,s+len1+1);
	reverse(t+1,t+len2+1); 
	Gethash1();
	for(int i=1;i<=len1;++i){
		for(int j=i;j<=len1;++j){
			pair<int,ull> tmp =Get(i,j);
			if(!rev.count(Get(i,j)))	rev[tmp]=1;
			else rev[tmp]++;
		}
	}
	Gethash2();
	for(int i=2;i<=len2;++i){
		for(int j=1;i+j-1<=len2;++j)	s1[j-1]=t[i+j-1];
		int now=len2-i+1;
		s1[now]='#';
		for(int j=now+1;j<=now+len2;++j)	s1[j]=t[j-now]; 
		
		for(int j=1;j<=now+len2;++j){
			int k=pi[i][j-1];
			while(k&&s1[k]!=s1[j])	k=pi[i][k-1];
			if(s1[k]==s1[j])	++k;
			pi[i][j]=k;
		}
		for(int j=now+1;j<=now+len2;++j)	pi[i][j-now]=pi[i][j];
	}
	for(int i=1;i<=len2;++i){
		for(int j=i;j<=len2;++j){
			ans+=1ll*rev[Get(i,j)]*(pi[j+1][i-1]);
		}
	}
	print(ans);
	return 0;
}
	

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 199208kb

input:

abab
ab

output:

8

result:

ok 1 number(s): "8"

Test #2:

score: 0
Accepted
time: 3ms
memory: 199244kb

input:

abab
abaaab

output:

29

result:

ok 1 number(s): "29"

Test #3:

score: 0
Accepted
time: 3ms
memory: 199352kb

input:

abcd
abcde

output:

10

result:

ok 1 number(s): "10"

Test #4:

score: 0
Accepted
time: 16ms
memory: 199392kb

input:

aaba
ba

output:

6

result:

ok 1 number(s): "6"

Test #5:

score: -100
Wrong Answer
time: 12ms
memory: 199432kb

input:

babababaaabbaabababbbaabbbababbaaaaa
aaaabbaababbab

output:

1488

result:

wrong answer 1st numbers differ - expected: '1161', found: '1488'