QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#442055#8475. Palindrome Stringsbulijiojiodibuliduo#WA 31ms521992kbC++202.8kb2024-06-15 06:05:182024-06-15 06:05:19

Judging History

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

  • [2024-06-15 06:05:19]
  • 评测
  • 测评结果:WA
  • 用时:31ms
  • 内存:521992kb
  • [2024-06-15 06:05:18]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(3); 
const ll mod=998244353;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=1010000;
int n,q;
char s[N],t[N];

int f1[N];

int f[N*2];	
namespace manacher {
void manacher(char* str,int n){ 
	static char s[N*2];
	rep(i,0,n+1) s[i*2]=str[i], s[i*2+1]='#';
	s[1]='>';
	s[0]='!'; s[2*n+2]='?';
	int a=0,p=0;    
	rep(i,1,2*n+2){
		int h=0;
		if(i<=a+p)h=min(f[2*a-i],a+p-i);
		while(s[i+h+1]==s[i-h-1])h++;
		f[i]=h;
		if(i+h>a+p)a=i,p=h;
	}
}
}

namespace sam {
const int N=1001000;
struct node {
	node *go[26],*p;
	vector<node*> son;
	int val,cnt;
	ll f1,f2;
}pool[2*N],*cur=pool,*rt;

node* newnode() {
	node *p=cur++;
	p->val=p->cnt=0; p->p=0;
	memset(p->go,0,sizeof(p->go));
	return p;
}
node *append(node *p,int w) {
	node *np=newnode();np->val=p->val+1;
	for (;p&&!p->go[w];p=p->p) p->go[w]=np;
	if (!p) np->p=rt;
	else {
		node *q=p->go[w];
		if (q->val==p->val+1) np->p=q;
		else {
			node *nq=newnode();
			nq->val=p->val+1;
			memcpy(nq->go,q->go,sizeof(q->go));
			nq->p=q->p;
			np->p=q->p=nq;
			for (;p&&p->go[w]==q;p=p->p) p->go[w]=nq;
		}
	}
	return np;
}
void init() {
	cur=pool;
	rt=newnode();
	node *np=rt;
	per(i,1,n+1) {
		np=append(np,s[i]-'a');
		np->cnt=i;
	}
	for (node *p=pool;p!=cur;p++) if (p->p) p->p->son.pb(p);
	function<void(node*)> dfs=[&](node *x) {
		if (x->cnt) {
			x->f1=1;
			x->f2=f1[x->cnt-1];
		}
		for (auto v:x->son) {
			dfs(v);
			x->f1+=v->f1;
			x->f2+=v->f2;
		}
	};
	dfs(rt);	
	//printf("!! %d %d\n",rt->f1,rt->f2);
}

}

int main() {
	scanf("%d%d",&n,&q);
	scanf("%s",s+1);
	manacher::manacher(s,n);
	for (int i=1;i<=n;i++) {
		f1[i]++;
		f1[i+f[2*i]/2+1]--;
		//printf("odd %d %d\n",i,i+f[2*i]/2);
	}
	for (int i=2;i<=n;i++) {
		f1[i]++;
		f1[i+(f[2*i-1]+1)/2]--;
		//printf("even %d %d\n",i,i+(f[2*i-1]+1)/2-1);
	}
	f1[1]++;
	rep(i,1,n+1) f1[i]+=f1[i-1];
	sam::init();
	rep(qq,0,q) {
		scanf("%s",t+1);
		int m=strlen(t+1);
		manacher::manacher(t,m);
		auto *r=sam::rt;
		ll ans=0;
		auto pal=[&](int l,int r) {
			//printf("%d %d %d\n",l,r,f[l+r]);
			return f[l+r]>=r-l+1;
		};
		rep(i,1,m+1) {
			if (r) r=r->go[t[i]-'a'];
			if (r&&i!=m&&pal(i+1,m)) ans+=r->f1;
		}
		if (r) ans+=r->f2;
		printf("%lld\n",ans);
	}

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 31ms
memory: 521992kb

input:

8 3
icpccamp
p
c
pc

output:

4
7
4

result:

ok 3 number(s): "4 7 4"

Test #2:

score: -100
Wrong Answer
time: 31ms
memory: 521916kb

input:

10 3
bbbabbbbbb
baaaa
abb
bb

output:

10
4
30

result:

wrong answer 3rd numbers differ - expected: '31', found: '30'