Codeoforces 271 D (suffix automatic machine SAM)

Source: Internet
Author: User

Question: Give You A string "S" and define whether each character is "good" or "bad". Calculate the number of different strings in "S" that contain no more than k bad characters.

 

Idea: hash and SET can be used for this question, but it is too slow. I think the positive solution should be SA or SAM. The following describes SAM's practice.

We construct the SAM of S, and maintain sum in every state of SAM, which indicates the number of "bad" characters in the sub-string in this state, po indicates the position where the substring in this status appears (any one ). We then sort the SAM topology and traverse it from the top down. When we traverse to a p state, we check the sum value of the par node in this state. If sum has exceeded k, obviously, all the substrings in this state do not meet the requirements. We may set sum of p to k + 1 and continue to traverse the next node. Otherwise, we set tmp = sum, mi is the minimum length of the substring represented by p (p-> par-> val + 1), ma is the maximum length of the substring represented by p (p-> val ), start from small to big to enumerate each sub-string, that is, from p-> po-mi + 1 to p-> po-ma + 1. If a "good" character is found, ans + = 1; otherwise, tmp ++. If tmp exceeds k, Set p-> sum = k + 1. Skip this state. Otherwise, ans + = 1, finally, set sum = tmp. Continue to traverse the next state. Finally, we can output ans. The Code is as follows:

[Cpp]
# Include <iostream>
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
# Define maxn3010
# Define Smaxn 26
Using namespace std;
Struct node
{
Node * par, * go [Smaxn];
Int po;
Int sum;
Int val;
} * Root, * tail, que [maxn], * top [maxn];
Int tot;
Char str [maxn> 1];
Int vis [26];
Void add (int c, int l, int po)
{
Node * p = tail, * np = & que [tot ++];
Np-> val = l;
Np-> po = po;
While (p & p-> go [c] = NULL)
P-> go [c] = np, p = p-> par;
If (p = NULL) np-> par = root;
Else
{
Node * q = p-> go [c];
If (p-> val + 1 = q-> val) np-> par = q;
Else
{
Node * nq = & que [tot ++];
* Nq = * q;
Nq-> val = p-> val + 1;
Np-> par = q-> par = nq;
While (p & p-> go [c] = q) p-> go [c] = nq, p = p-> par;
}
}
Tail = np;
}
Int c [maxn], len;
Void init ()
{
Len = 1;
Tot = 0;
Memset (que, 0, sizeof (que ));
Root = tail = & que [tot ++];
}
Void solve (int limit)
{
Int I, j;
Memset (c, 0, sizeof (c ));
For (I = 0; I <tot; I ++)
C [que [I]. val] ++;
For (I = 1; I <len; I ++)
C [I] + = c [I-1];
For (I = 0; I <tot; I ++)
Top [-- c [que [I]. val] = & que [I];
Int sum = 0;
For (I = 1; I <tot; I ++)
{
Node * p = top [I];
If (p-> par-> sum> limit)
{
P-> sum = limit + 1;
Continue;
}
Int mi = p-> par-> val + 1, ma = p-> val, tmp = p-> par-> sum, po = p-> po;
For (j = mi; j <= ma; j ++)
{
If (vis [str [po-j + 1]-'a'])
{
Tmp ++;
If (tmp> limit)
{
Break;
}
Else
Sum ++;
}
Else
Sum ++;
}
P-> sum = tmp;
}
Printf ("% d \ n", sum );
}
Int main ()
{
// Freopen ("dd.txt", "r", stdin );
Scanf ("% s", str );
Int I, k, l = strlen (str );
Init ();
For (I = 0; I <l; I ++)
{
Add (str [I]-'A', len ++, I );
}
Char tmp [26];
Scanf ("% s", tmp );
For (I = 0; I <26; I ++)
Vis [I] = 1-(tmp [I]-'0 ');
Scanf ("% d", & k );
Solve (k );
Return 0;
}

# Include <iostream>
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
# Define maxn3010
# Define Smaxn 26
Using namespace std;
Struct node
{
Node * par, * go [Smaxn];
Int po;
Int sum;
Int val;
} * Root, * tail, que [maxn], * top [maxn];
Int tot;
Char str [maxn> 1];
Int vis [26];
Void add (int c, int l, int po)
{
Node * p = tail, * np = & que [tot ++];
Np-> val = l;
Np-> po = po;
While (p & p-> go [c] = NULL)
P-> go [c] = np, p = p-> par;
If (p = NULL) np-> par = root;
Else
{
Node * q = p-> go [c];
If (p-> val + 1 = q-> val) np-> par = q;
Else
{
Node * nq = & que [tot ++];
* Nq = * q;
Nq-> val = p-> val + 1;
Np-> par = q-> par = nq;
While (p & p-> go [c] = q) p-> go [c] = nq, p = p-> par;
}
}
Tail = np;
}
Int c [maxn], len;
Void init ()
{
Len = 1;
Tot = 0;
Memset (que, 0, sizeof (que ));
Root = tail = & que [tot ++];
}
Void solve (int limit)
{
Int I, j;
Memset (c, 0, sizeof (c ));
For (I = 0; I <tot; I ++)
C [que [I]. val] ++;
For (I = 1; I <len; I ++)
C [I] + = c [I-1];
For (I = 0; I <tot; I ++)
Top [-- c [que [I]. val] = & que [I];
Int sum = 0;
For (I = 1; I <tot; I ++)
{
Node * p = top [I];
If (p-> par-> sum> limit)
{
P-> sum = limit + 1;
Continue;
}
Int mi = p-> par-> val + 1, ma = p-> val, tmp = p-> par-> sum, po = p-> po;
For (j = mi; j <= ma; j ++)
{
If (vis [str [po-j + 1]-'a'])
{
Tmp ++;
If (tmp> limit)
{
Break;
}
Else
Sum ++;
}
Else
Sum ++;
}
P-> sum = tmp;
}
Printf ("% d \ n", sum );
}
Int main ()
{
// Freopen ("dd.txt", "r", stdin );
Scanf ("% s", str );
Int I, k, l = strlen (str );
Init ();
For (I = 0; I <l; I ++)
{
Add (str [I]-'A', len ++, I );
}
Char tmp [26];
Scanf ("% s", tmp );
For (I = 0; I <26; I ++)
Vis [I] = 1-(tmp [I]-'0 ');
Scanf ("% d", & k );
Solve (k );
Return 0;
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.