CF873F Forbidden Indices 后缀自动机+水题

刷刷水~

Code:

#include <cstdio> 
#include <cstring> 
#include <algorithm>
#define N 200005
#define setIO(s) freopen(s".in","r",stdin) 
using namespace std;  
char str1[N],str2[N]; 
int tot,last,n,c[N<<1],rk[N<<1]; 
struct Node 
{
    int ch[26],f,len,cnt; 
}t[N<<1];    
void extend(int c,int flag) 
{
    int np=++tot,p=last; 
    last=np,t[np].len=t[p].len+1;     
    for(;p&&!t[p].ch[c];p=t[p].f) t[p].ch[c]=np;    
    if(!p) t[np].f=1; 
    else
    {
        int q=t[p].ch[c]; 
        if(t[q].len==t[p].len+1) t[np].f=q; 
        else 
        {
            int nq=++tot; 
            t[nq].len=t[p].len+1; 
            t[nq].f=t[q].f,t[q].f=t[np].f=nq;    
            memcpy(t[nq].ch,t[q].ch,sizeof(t[q].ch));   
            for(;p&&t[p].ch[c]==q;p=t[p].f) t[p].ch[c]=nq;    
        }
    }
    t[np].cnt=flag;  
}
int main() 
{ 
    int i,j; 
    long long answer=0; 
    // setIO("input");  
    scanf("%d%s%s",&n,str1+1,str2+1); 
    for(last=tot=i=1;i<=n;++i) extend(str1[i]-'a',str2[i]=='0'?1:0);    
    for(i=1;i<=tot;++i) ++c[t[i].len]; 
    for(i=1;i<=tot;++i) c[i]+=c[i-1];
    for(i=1;i<=tot;++i) rk[c[t[i].len]--]=i;    
    for(i=tot;i>=1;--i) 
    {  
        int p,ff; 
        p=rk[i],ff=t[p].f;   
        answer=max(answer,(long long)t[p].len*t[p].cnt);    
        t[ff].cnt+=t[p].cnt;   
    }
    printf("%lld
",answer);    
    return 0; 
}

  

原文地址:https://www.cnblogs.com/guangheli/p/11478991.html