2018年10月20日提高組 T3 好文章
阿新 • • 發佈:2018-12-16
大意
判斷在一個長度為的串中長度為的不同連續子串個數
思路
裸的,然而被卡了,所以要用 雙模數
程式碼
#include<cstdio>
#include<algorithm>
#define ymw 1000000007
#define bpm 1000000009//雙模數
using namespace std;typedef unsigned long long ull;
int n,m,len,ans=1;
ull h[200001],c[200001];
char s[200001];
struct node
{
int x,y;
bool operator<(node h) const
{
return x==h.x?y<h.y:x<h.x;
}
}a[200001];
signed main()
{
scanf("%d%d\n",&n,&m);
gets(s+1);
c[0]=1;
for(register int i=1;i<=n;i++)
{
h[i]=(h[i-1]*437+s[i]-96)%ymw;
c[i]=c[i-1]*437%ymw;
}
for(register int i=1;i+m-1<=n;i++) a[i].x=(h[i+m-1]-h[i-1]*c[m]%ymw+ymw)% ymw;
c[0]=1;h[0]=0;
for(register int i=1;i<=n;i++)
{
h[i]=(h[i-1]*439+s[i]-96)%bpm;
c[i]=c[i-1]*439%bpm;
}
for(register int i=1;i+m-1<=n;i++) a[i].y=(h[i+m-1]-h[i-1]*c[m]%bpm+bpm)%bpm;
sort(a+1,a+1+n-m+1);
for(register int i=2;i+m-1<=n;i++) ans+=(a[i].x!=a[i-1].x||a[i].y!=a[i-1].y); //離散化
printf("%d",ans);//輸出
}