QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#236635#7512. Almost Prefix Concatenationfzj2007WA 8ms89372kbC++173.1kb2023-11-04 09:17:502023-11-04 09:17:50

Judging History

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

  • [2023-11-04 09:17:50]
  • 评测
  • 测评结果:WA
  • 用时:8ms
  • 内存:89372kb
  • [2023-11-04 09:17:50]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
template<typename T>inline void read(T &x){
    x=0;
    char ch=getchar();
    bool flag=0;
    while(ch>'9'||ch<'0') flag=flag||ch=='-',ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    x=flag?-x:x;
}
template<typename T,typename ...Args>inline void read(T &x,Args &...args){
    read(x),read(args...);
}
template<typename T>inline void prt(T x){
    if(x>9) prt(x/10);
    putchar(x%10+'0');
}
template<typename T>inline void put(T x){
    if(x<0) putchar('-'),x=-x;
    prt(x);
}
template<typename T>inline void put(char ch,T x){
    put(x),putchar(ch);
}
template<typename T,typename ...Args>inline void put(char ch,T x,Args ...args){
    put(ch,x),put(ch,args...);
}
#define N 2000005
#define ll long long
namespace SA{
	char s[N];
	int x[N],y[N],sa[N],rk[N],h[N],c[N],g[N][23],lg[N],n;
	inline void build(){
		int m=300;
		for(int i=2;i<=n;i++) lg[i]=lg[i>>1]+1;
		for(int i=1;i<=n;i++) c[x[i]=s[i]]++;
		for(int i=1;i<=m;i++) c[i]+=c[i-1];
		for(int i=n;i;i--) sa[c[x[i]]--]=i;
		for(int k=1;k<=n;k<<=1){
			int num=0;
			for(int i=n-k+1;i<=n;i++) y[++num]=i;
			for(int i=1;i<=n;i++)
				if(sa[i]>k) y[++num]=sa[i]-k;
			for(int i=1;i<=m;i++) c[i]=0;
			for(int i=1;i<=n;i++) c[x[i]]++;
			for(int i=1;i<=m;i++) c[i]+=c[i-1];
			for(int i=n;i;i--) sa[c[x[y[i]]]--]=y[i],y[i]=0;
			swap(x,y);
			x[sa[1]]=num=1;
			for(int i=2;i<=n;i++)
				x[sa[i]]=(y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k])?num:++num;
			if(num==n) break;
			m=num;
		}
		for(int i=1;i<=n;i++) rk[sa[i]]=i;
		for(int i=1,p=0;i<=n;i++){
			if(p) p--;
			while(s[i+p]==s[sa[rk[i]-1]+p]) p++;
			h[rk[i]]=p;
		}
		for(int i=1;i<=n;i++) g[i][0]=h[i];
		for(int j=1;j<=lg[n];j++)
			for(int i=1;i+(1<<j)-1<=n;i++) g[i][j]=min(g[i][j-1],g[i+(1<<j-1)][j-1]);
	}
	inline int lcp(int i,int j){
		if(i==j) return n-i+1;
		int l=rk[i],r=rk[j],k;
		if(l>r) swap(l,r);
		l++,k=lg[r-l+1];
		return min(g[l][k],g[r-(1<<k)+1][k]);
	} 
}
using SA::lcp;
char A[N],B[N];
int n,m,L[N],dis[N];
struct BIT{
	ll c[N];
	BIT(){memset(c,0,sizeof(c));}
	#define lowbit(x) (x&-x)
	inline void update(int x,ll v){
		x++;
		for(;x<=n+1;x+=lowbit(x)) c[x]+=v;
	}
	inline void update(int l,int r,ll v){
		update(l,v),update(r+1,-v);
	}
	inline ll query(int x){
		ll res=0;
		x++;
		for(;x;x^=lowbit(x)) res+=c[x];
		return res;
	}
	#undef lowbit
}T0,T1,T2;
int main(){
	scanf("%s%s",A+1,B+1);
	n=strlen(A+1),m=strlen(B+1);
	for(int i=1;i<=n;i++) SA::s[i]=A[i];
	SA::s[n+1]='#';
	for(int i=1;i<=m;i++) SA::s[i+n+1]=B[i];
	SA::n=n+m+1,SA::build();
	for(int i=1;i<=n;i++) L[i]=lcp(i,n+2);
	for(int i=1;i<=n;i++){
		if(L[i]>=m||i+L[i]>n){
			dis[i]=L[i];
			continue; 
		}
		dis[i]=L[i]+1+(L[i]+2<=m&&i+L[i]+1<=n?lcp(i+L[i]+1,n+3+L[i]):0);
	}
	T0.update(0,0,1);
	for(int i=1;i<=n;i++){
		int l=i,r=i+dis[i]-1;
		if(l>r) continue;
		ll res0=T0.query(i-1),res1=T1.query(i-1),res2=T2.query(i-1);
		T0.update(l,r,res0);
		T1.update(l,r,res0+res1);
		T2.update(l,r,res0+res1*2+res2);
	}
	put('\n',T2.query(n));
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 88292kb

input:

ababaab
aba

output:

473

result:

ok 1 number(s): "473"

Test #2:

score: 0
Accepted
time: 7ms
memory: 88896kb

input:

ac
ccpc

output:

5

result:

ok 1 number(s): "5"

Test #3:

score: -100
Wrong Answer
time: 8ms
memory: 89372kb

input:

qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq...

output:

0

result:

wrong answer 1st numbers differ - expected: '75038697', found: '0'