QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#455295#7008. Rikka with Nice Counting Striking Backjinqihao2023AC ✓4184ms96464kbC++143.6kb2024-06-26 08:20:102024-06-26 08:20:11

Judging History

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

  • [2024-06-26 08:20:11]
  • 评测
  • 测评结果:AC
  • 用时:4184ms
  • 内存:96464kb
  • [2024-06-26 08:20:10]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=4e5+5,p1=19491001,mod1=1e9+7,M=2e6+5,p2=131,mod2=998244353,K=2e6+5;
int T,n,pw1[N],h1[N],pw2[N],h2[N];
char s[N];
int hash1(int l,int r){return (h1[r]-(ll)h1[l-1]*pw1[r-l+1]%mod1+mod1)%mod1;}
int hash2(int l,int r){return (h2[r]-(ll)h2[l-1]*pw2[r-l+1]%mod2+mod2)%mod2;}
int lcp(int i,int j)
{
	int l=1,r=min(n-i+1,n-j+1),pos=0;
	while(l<=r)
	{
		int mid=l+r>>1;
		if(hash1(i,i+mid-1)==hash1(j,j+mid-1) && hash2(i,i+mid-1)==hash2(j,j+mid-1))pos=mid,l=mid+1;
		else r=mid-1;
	}
	return pos;
}
int lcs(int i,int j)
{
	int l=1,r=min(i,j),pos=0;
	while(l<=r)
	{
		int mid=l+r>>1;
		if(hash1(i-mid+1,i)==hash1(j-mid+1,j) && hash2(i-mid+1,i)==hash2(j-mid+1,j))pos=mid,l=mid+1;
		else r=mid-1;
	}
	return pos;
}
int cmp(int i,int j)
{
	int num=lcp(i,j);
	if(s[i+num]>s[j+num])return 1;
	if(s[i+num]==s[j+num])return 0;
	return -1;
}
int st[N],ct,top;
struct abc{int l,r,p;}runs[N];
bool cmp1(abc a,abc b){if(a.l!=b.l)return a.l<b.l;if(a.r!=b.r)return a.r<b.r;return a.p<b.p;}
void get_runs()
{
	ct=0,st[0]=n+1,top=0;
	for(int i=n;i>=1;i--)
	{
		while(top && cmp(i,st[top])<=0)top--;
		int l=i,r=st[top]-1,nl=l-lcs(l-1,r),nr=r+lcp(l,r+1);
		if(nr-nl+1>=(r-l+1)*2)ct++,runs[ct]=(abc){nl,nr,r-l+1};
		st[++top]=i;
	}
	top=0;
	for(int i=n;i>=1;i--)
	{
		while(top && cmp(i,st[top])>=0)top--;
		int l=i,r=st[top]-1,nl=l-lcs(l-1,r),nr=r+lcp(l,r+1);
		if(nr-nl+1>=(r-l+1)*2)ct++,runs[ct]=(abc){nl,nr,r-l+1};
		st[++top]=i;
	}
	sort(runs+1,runs+ct+1,cmp1);int ct1=0;
	for(int i=1;i<=ct;i++)if(runs[i].l!=runs[i-1].l || runs[i].r!=runs[i-1].r)ct1++,runs[ct1]=runs[i];
	ct=ct1;
}
struct hash_table
{
	int h[M],nxt[M],cnt;
	pair<int,int>val[M];
	int cr[M];
	void insert(pair<int,int>v)
	{
		int x=(v.first+v.second)%M;
		cr[++cr[0]]=x;
		for(int i=h[x];i;i=nxt[i])if(val[i]==v)return ;
		cnt++,nxt[cnt]=h[x],val[cnt]=v,h[x]=cnt;
	}
	void clr()
	{
		for(int i=1;i<=cr[0];i++)h[cr[i]]=0;
		cr[0]=0,cnt=0;
	}
}t1;
struct SAM
{
	int tot;
	struct Tree{int ch[26],fa,len,sz;}t[N*4];
	void clr(){for(int i=0;i<=tot;i++){for(int j=0;j<26;j++)t[i].ch[j]=0;t[i].fa=t[i].len=0;}tot=0;}
	ll build()
	{
		int lst=0;
		for(int i=1;i<=n;i++)
		{
			// cout<<i<<endl;
			int now=++tot,p=lst,k=s[i]-'a';
			t[now].len=t[p].len+1,t[now].sz=1;
			while(1)
			{
				if(!t[p].ch[k])t[p].ch[k]=now;
				else
				{
					int q=t[p].ch[k];
					if(t[q].len==t[p].len+1)t[now].fa=q;
					else
					{
						int nq=++tot;
						t[nq]=t[q],t[nq].sz=0,t[nq].len=t[p].len+1,t[q].fa=t[now].fa=nq;
						while(t[p].ch[k]==q){t[p].ch[k]=nq;if(!p)break;p=t[p].fa;}
					}
					break;
				}
				if(!p)break;
				p=t[p].fa;
			}
			lst=now;
		}
		ll num=0;
		for(int i=1;i<=tot;i++)num+=t[i].len-t[t[i].fa].len;
		return num;
	}
}sam;
void solve()
{
	// scanf("%d",&n);
	scanf("%s",s+1);
	n=strlen(s+1);
	s[0]=s[n+1]='a'-1;
	for(int i=1;i<=n;i++)h1[i]=((ll)h1[i-1]*p1+s[i])%mod1;
	for(int i=1;i<=n;i++)h2[i]=((ll)h2[i-1]*p2+s[i])%mod2;
	get_runs(),t1.clr();
	for(int i=1;i<=ct;i++)
	{
		// cout<<runs[i].l<<" "<<runs[i].r<<" "<<runs[i].p<<endl;
		for(int j=runs[i].l;j<runs[i].l+runs[i].p;j++)
		{
			for(int k=j+2*runs[i].p-1;k<=runs[i].r;k+=runs[i].p)
			{
				t1.insert({hash1(j,k),hash2(j,k)});
			}
		}
	}
	sam.clr();
	ll res=sam.build();
	// cout<<res<<endl;
	res-=t1.cnt;
	printf("%lld\n",res);
}
int main()
{
	pw1[0]=1;for(int i=1;i<N;i++)pw1[i]=(ll)pw1[i-1]*p1%mod1;
	pw2[0]=1;for(int i=1;i<N;i++)pw2[i]=(ll)pw2[i-1]*p2%mod2;
	// freopen("book.in","r",stdin);
	// freopen("book.out","w",stdout);
	scanf("%d",&T);
	while(T--)solve();
	return 0;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

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

input:

6
rikkasuggeststoallthecontestants
thisisaproblemdesignedforgrandmasters
ifyoudidnotachievethat
youdbetterskiptheproblem
wishyouahighrank
enjoytheexperience

output:

500
679
244
290
132
163

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 3587ms
memory: 96464kb

input:

1000
mxsslrrxtwwnwlsrxxxbsmtxxwwntsbrwlxnbxunutnlmrrlubturuwmbnobrolwunwurtbnmlumxmwtwrsulruxslsnltsstmmxbsmuonroubrnnmu
sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
glggglgg
yykkyyyykykykyyyykyykykkkyyyykkyykkkyyykykyykyyykkykky...

output:

6522
1
20
9443
11294
1
8619
26
7898
260905
9048
6
22114
52
20
45
7
39
10
1
28
26
10
47
32
12977
30
13
7473
12
8402
1
8083
16
1
10462
16
9278
1
1
8968
7858
11148
8130
6
8516
12223
9266
8374
26
45
14
10150
9
10175
298758
203677
11522
12
8985
10687
12
1
6613277686
10
10
1
5794
28
1
280529
7874
13
10564...

result:

ok 1000 lines

Test #3:

score: 0
Accepted
time: 4156ms
memory: 89088kb

input:

26
hihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihh...

output:

9523687993
9529757593
9537405289
9539347561
9533035177
9528058105
564250
9522959641
9542382361
9518953705
9519439273
9534977449
9519803449
9535705801
9519560665
9534249097
9527572537
9523687993
9539468953
9532064041
9525873049
9532185433
9541168441
9524901913
9531092905
9518832313

result:

ok 26 lines

Test #4:

score: 0
Accepted
time: 4184ms
memory: 89364kb

input:

26
oohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohooh...

output:

9625902365
9628810517
9622671085
9626467839
9628891299
9626306275
9630668503
9620409189
9618228075
9622428739
9628406607
9625336891
9630426157
9626871749
9620086061
9626144711
9616935563
9617177909
9626790967
9627194877
9626467839
354971
9616370089
9618308857
9617824165
9616773999

result:

ok 26 lines

Test #5:

score: 0
Accepted
time: 3682ms
memory: 83940kb

input:

26
vggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvvggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvvggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvvggvgvggvgvvgvggvgvggvg...

output:

13085098340
13073668733
13071665606
13067070197
13077910649
13074964874
13078735466
13070840789
13072726085
13067895014
669720
13065891887
13065302732
13076496677
13068484169
13064242253
13065420563
13063181774
13080502931
13067070197
13072490423
13070015972
13083802199
13064831408
13075671860
13085...

result:

ok 26 lines