Forbidden Indices CodeForces - 873F

You are given a string s consisting of n lowercase Latin letters. Some indices in this string are marked asforbidden.

You want to find a string a such that the value of |af(a) is maximum possible, where f(a) is the number of occurences of a in s such that these occurences end in non-forbidden indices. So, for example, if s isaaaa, a is aa and index 3 is forbidden, then f(a) = 2 because there are three occurences of a in s (starting in indices 1, 2 and 3), but one of them (starting in index 2) ends in a forbidden index.

Calculate the maximum possible value of |af(a) you can get.

Input

The first line contains an integer number n (1 ≤ n ≤ 200000) — the length of s.

The second line contains a string s, consisting of n lowercase Latin letters.

The third line contains a string t, consisting of n characters 0 and 1. If i-th character in t is 1, then i is a forbidden index (otherwise i is not forbidden).

Output

Print the maximum possible value of |af(a).

Examples

Input
5
ababa
00100
Output
5
Input
5
ababa
00000
Output
6
Input
5
ababa
11111
Output
0


后缀自动机裸题

 1 #include<bits/stdc++.h>
 2 using namespace std; 
 3 int const N=200000+10;  
 4 struct node{
 5     int len,fa,ch[26];  
 6 }a[N<<1]; 
 7 int tot,ls,sz[N<<1],num[N],sa[N<<1]; 
 8 char s[N],s2[N]; 
 9 void add(int c,int v){
10     int p=ls;  
11     int np=ls=++tot;  
12     a[np].len=a[p].len+1; 
13     sz[np]=v;  
14     for(;p&&!a[p].ch[c];p=a[p].fa) a[p].ch[c]=np;  
15     if(!p) a[np].fa=1;  
16     else {    
17         int q=a[p].ch[c];  
18         if(a[q].len==a[p].len+1) a[np].fa=q;  
19         else {
20             int nq=++tot;  
21             a[nq]=a[q];  
22             a[nq].len=a[p].len+1;  
23             a[np].fa=a[q].fa=nq;  
24             for(;p&&a[p].ch[c]==q;p=a[p].fa) 
25                 a[p].ch[c]=nq;  
26         } 
27     }
28 }      
29 int main(){
30     int len;  
31     scanf("%d",&len); 
32     scanf("%s",s+1);   
33     scanf("%s",s2+1);  
34     tot=ls=1;  
35     for(int i=1;i<=len;i++)  
36         add(s[i]-'a','1'-s2[i]);   
37     for(int i=1;i<=tot;i++) num[a[i].len]++;  
38     for(int i=1;i<=len;i++) num[i]+=num[i-1];  
39     for(int i=1;i<=tot;i++) sa[num[a[i].len]--]=i;  
40     for(int i=tot;i>=1;i--) {    
41         int x=sa[i];  
42         int f=a[x].fa; 
43         sz[f]+=sz[x];  
44     }  
45     long long  ans=0;  
46     for(int i=2;i<=tot;i++)  
47         ans=max(ans,1LL*a[i].len*sz[i]);  
48     printf("%lld\n",ans);  
49     return 0; 
50 }  
View Code

 

posted @ 2019-06-21 00:19  zjxxcn  阅读(154)  评论(0编辑  收藏  举报