提交记录 9001


用户 题目 状态 得分 用时 内存 语言 代码长度
Jouna_Kasa_Hasinele noi18c. 【NOI2018】你的名字 Accepted 100 1.054 s 493048 KB C++11 3.74 KB
提交时间 评测时间
2019-04-01 15:20:16 2020-08-01 01:28:47
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1000005,M=2000005,K=26,E=20000005;
int n,m,q,ql,qr;
char s[N],t[N];
int lim[N];
namespace sams
{
	int ind,la,tot;
	int dir[N][K],len[N],fail[N],fro[N],fr[N],nxt[N],em[N],lc[E],rc[E],now[E],sye[E],poe[N];
	inline void addedge(int u,int v)
	{
		static int tot=0;
		++tot;
		nxt[tot]=fr[u];
		fr[u]=tot;
		em[tot]=v;
	}
	#define mid ((l+r)>>1)
	void ins(int &p,int l,int r,int q,int x)
	{
		if(p==0)
		{
			p=++tot;
			lc[p]=rc[p]=now[p]=0;
			sye[p]=x;
		}
		if(l==r)
		{
			now[p]++;
			return;
		}
		if(q<=mid)
			ins(lc[p],l,mid,q,x);
		else
			ins(rc[p],mid+1,r,q,x);
		now[p]=now[lc[p]]+now[rc[p]];
	}
	int query(int p,int l,int r,int ql,int qr)
	{
		if(p==0)
			return 0;
		if(ql<=l&&r<=qr)
			return now[p];
		int ans=0;
		if(l<=qr&&ql<=mid)
			ans+=query(lc[p],l,mid,ql,qr);
		if(mid+1<=qr&&ql<=r)
			ans+=query(rc[p],mid+1,r,ql,qr);
		return ans;
	}
	inline int merge(int p,int q,int l,int r,int x)
	{
		if(p==0)
			return q;
		if(q==0)
			return p;
		if(sye[p]==x)
			now[p]+=now[q];
		else
		{
			++tot;
			lc[tot]=lc[p];
			rc[tot]=rc[p];
			now[tot]=now[p];
			sye[tot]=x;
			p=tot;
			now[p]+=now[q];
		}
		if(l==r)
			return p;
		lc[p]=merge(lc[p],lc[q],l,mid,x);
		rc[p]=merge(rc[p],rc[q],mid+1,r,x);
		now[p]=now[lc[p]]+now[rc[p]];
		return p;
	}
	#undef mid
	void ins(int c,int le)
	{
		++ind;
		ins(fro[ind],1,n,le,ind);
		poe[ind]=le;
		len[ind]=len[la]+1;
		while(la!=0&&dir[la][c]==0)
		{
			dir[la][c]=ind;
			la=fail[la];
		}
		if(la==0&&dir[la][c]==0)
		{
			dir[la][c]=ind;
			fail[ind]=la;
			la=ind;
			return;
		}
		else if(len[dir[la][c]]==len[la]+1)
		{
			fail[ind]=dir[la][c];
			la=ind;
			return;
		}
		else
		{
			int p=dir[la][c];
			++ind;
			len[ind]=len[la]+1;
			fail[ind]=fail[p];
			poe[ind]=poe[p];
			ins(fro[ind],1,n,poe[ind],ind);
			memmove(dir[ind],dir[p],sizeof(dir[ind]));
			fail[p]=ind;
			fail[ind-1]=ind;
			for(int j=la;dir[j][c]==p;j=fail[j])
				dir[j][c]=ind;
			la=ind-1;
			return;
		}
	}
	void build(int d)
	{
		for(int j=fr[d];j!=0;j=nxt[j])
		{
			build(em[j]);
			fro[d]=merge(fro[d],fro[em[j]],1,n,d);
		}
	}
};
namespace samt
{
	int ind,la;
	int dir[M][K],len[M],fail[M],pos[M];
	inline void clear()
	{
		ind=la=0;
		memset(dir[0],0,sizeof(dir[0]));
	}
	void ins(int c,int le)
	{
		++ind;
		memset(dir[ind],0,sizeof(dir[ind]));
		pos[ind]=le;
		len[ind]=len[la]+1;
		while(la!=0&&dir[la][c]==0)
		{
			dir[la][c]=ind;
			la=fail[la];
		}
		if(la==0&&dir[la][c]==0)
		{
			dir[la][c]=ind;
			fail[ind]=la;
			la=ind;
			return;
		}
		else if(len[dir[la][c]]==len[la]+1)
		{
			fail[ind]=dir[la][c];
			la=ind;
			return;
		}
		else
		{
			int p=dir[la][c];
			++ind;
			len[ind]=len[la]+1;
			fail[ind]=fail[p];
			pos[ind]=pos[p];
			memmove(dir[ind],dir[p],sizeof(dir[ind]));
			fail[p]=ind;
			fail[ind-1]=ind;
			for(int j=la;dir[j][c]==p;j=fail[j])
				dir[j][c]=ind;
			la=ind-1;
			return;
		}
	}
};
int main()
{
	scanf("%s",s+1);
	n=strlen(s+1);
	for(int i=1;i<=n;i++)
		sams::ins(s[i]-'a',i);
	for(int i=1;i<=sams::ind;i++)
		sams::addedge(sams::fail[i],i);
	sams::build(0);
	scanf("%d",&q);
	while(q--)
	{
		scanf("%s",t+1);
		m=strlen(t+1);
		scanf("%d%d",&ql,&qr);
		samt::clear();
		for(int i=1;i<=m;i++)
			samt::ins(t[i]-'a',i);
		for(int i=1,now=0,le=0;i<=m;i++)
		{
			while(1)
			{
				if(sams::dir[now][t[i]-'a']!=0&&sams::query(sams::fro[sams::dir[now][t[i]-'a']],1,n,ql+le,qr)>0)
				{
					now=sams::dir[now][t[i]-'a'];
					le++;
					break;
				}
				if(le==0)
					break;
				le--;
				if(le==sams::len[sams::fail[now]])
					now=sams::fail[now];
			}
			lim[i]=le;
		}
		ll ans=0;
		for(int i=1;i<=samt::ind;i++)
			ans+=max(0,samt::len[i]-max(samt::len[samt::fail[i]],lim[samt::pos[i]]));
		printf("%lld\n",ans);
	}
	return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #12.65 ms212 KBAcceptedScore: 4

Testcase #23.585 ms660 KBAcceptedScore: 4

Testcase #33.758 ms664 KBAcceptedScore: 4

Testcase #440.791 ms1 MB + 88 KBAcceptedScore: 4

Testcase #539.383 ms1 MB + 72 KBAcceptedScore: 4

Testcase #6870.702 ms481 MB + 504 KBAcceptedScore: 4

Testcase #7871 ms480 MB + 720 KBAcceptedScore: 4

Testcase #8140.452 ms75 MB + 332 KBAcceptedScore: 4

Testcase #9141.562 ms60 MB + 440 KBAcceptedScore: 4

Testcase #10303.3 ms145 MB + 408 KBAcceptedScore: 4

Testcase #11332.297 ms124 MB + 324 KBAcceptedScore: 4

Testcase #12492.413 ms216 MB + 996 KBAcceptedScore: 4

Testcase #13550.471 ms192 MB + 968 KBAcceptedScore: 4

Testcase #14683.858 ms293 MB + 84 KBAcceptedScore: 4

Testcase #15784.255 ms265 MB + 68 KBAcceptedScore: 4

Testcase #16892.599 ms369 MB + 352 KBAcceptedScore: 4

Testcase #171.025 s338 MB + 188 KBAcceptedScore: 4

Testcase #18528.304 ms153 MB + 308 KBAcceptedScore: 4

Testcase #19697.246 ms223 MB + 24 KBAcceptedScore: 4

Testcase #20873.553 ms295 MB + 288 KBAcceptedScore: 4

Testcase #211.023 s369 MB + 852 KBAcceptedScore: 4

Testcase #221.054 s369 MB + 184 KBAcceptedScore: 4

Testcase #231.05 s369 MB + 120 KBAcceptedScore: 4

Testcase #241.039 s369 MB + 728 KBAcceptedScore: 4

Testcase #251.033 s369 MB + 892 KBAcceptedScore: 4


Judge Duck Online | 评测鸭在线
Server Time: 2024-12-05 10:25:58 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠