The problem to Slow (palindromic Tree)

Source: Internet
Author: User

Title Link: http://codeforces.com/gym/100548

This evening suddenly had some interest to learn the data structure, and then on the various inadvertently saw the palindrome tree of information structures, is said to be the new data structure in 2014, but also reminds me of Xi ' an iron time experience. The test instructions of this problem is actually quite clear, give you two length 200000 string, ask them to have how many pairs palindrome substring. Processing strings There are many common tools, such as suffix arrays, suffix automata, ac automata, KMP, but these data structures are not particularly strong for palindrome processing, of course, a little bit of processing we can still good use of the above data structure to deal with some palindrome problems. Another problem with palindrome is to ask for the longest common substring of a string, and an O (n) Manachar algorithm solves this problem. Palindrome tree seems to be specifically designed to fill the blanks to deal with Palindrome, and most of all, when you have the basis of some of the previous data structures, and then to see the structure of the data structure, algorithm flow, you will feel very clear, very easy to understand, Of course this is thanks to the following blog gives me the code template and some understanding of the data structure.

http://blog.csdn.net/u013368721/article/details/42100363

http://blog.csdn.net/u013368721/article/details/42104207

Here are some of your personal understanding of the content of the above blog

The first is a simple description of the members of the data structure:
NXT[I][CH] node I points to the node after it has received the character Ch
Fail[i] A palindrome string ending at the end of a palindrome string referred to by node I fail[ABABA] = ABA Fail[aba] = a
Cnt[i] The number of palindrome strings represented by node I
Num[i] The number of palindrome ends at the end of a palindrome string represented by node I
Len[i] node I represents the length of the palindrome string
S[i] the first character
Last node of the longest palindrome generated by the newly added character
The total number of P nodes
n the length of the current string

One of the more difficult to understand is that fail[i]. If I node refers to the palindrome string is "Ababa" words, then fail[i] refers to the end of this palindrome string end (that is, ' a ') the last of the longest palindrome string, that is, ABA, and then the last natural is a. Similar is the mismatch pointer in the KMP, or the last suffix that can be received in the suffix automaton.

The next step is the description of the entire algorithm.

1. Start with the initialization of node No. 0 and number 1th, respectively, the length of the number of nodes (that is, empty string), and 1th node (the length of an odd number of empty strings), obviously the length of the empty string is 0, and such an odd string does not exist, len[0]=0,len[1]=-1. This len[1] 1 has its own advantages, and some of the benefits can be described in the blog above.

2. Next consider adding 1 new characters ch, the first thing to note is the function of the Getfail () function, while (S[n-len[x]-1]! = S[n]) x = fail[x]; The actual meaning is to find a can receive the current new character of the palindrome string number, the original string "Abacaba" new addition 1 C,s[n-len[x]-1]=a! = C, so go back to the previous palindrome string node, that is, the node of ABA, this time s[n-len[x]-1]=c, Meet test instructions, so we find a node that can receive this new state cur

3. Next we can update nxt[cur][ch], when this node does not exist, we need to create a new node, the length is len[cur]+2, and then we need to know the new node now the Fail value, Fail[now]=nxt[getfail (Fail[cur]) [CH], which is the last palindrome of the currently established node, Getfail (Fail[cur]) returns a palindrome string that can receive CH at the end of the corresponding string of fail[cur]. So you can understand Now=nxt[getfail (cur)][ch],fail[now]=nxt[getfail (fail[cur])][ch].

4. Then update the NUM value of the new node, as well as the CNT value, note that the CNT value here does not represent the total number of occurrences of the palindrome substring of the node, as in the case of the suffix automaton, CNT does not represent the total number of occurrences of this string, and finally requires a result to be added to the process, That is the process of count.

Finally the entire code is a relatively clear structure, the following code for this problem is used above the template provided by the blog.

For the original topic, need to do is in a string of the center node 0, and the odd String center node 1, and constantly to the two Frontier fortress characters, when both sides of the state are there to continue to DFS, the final statistic results can be.

The information that this data structure maintains can actually be used for many special purposes.

1. Direct DFS (0), DFS (1) We can print out all the palindrome strings and the number of occurrences each.

2. Whether the new 1 characters can produce a new palindrome string, that is, the number of different palindrome substrings that can be drawn online to the prefix I of S contains.

3.num[i] can get the number of palindrome strings at the end of each new character, and Len[i] find the longest palindrome length at the end of this character.

In a word, I think this data structure can do most of the problem of palindrome string, and this data structure is very elegent. The string length is n, the character set size is m, the space complexity is O (nm), as for the time complexity, I do not know how to prove, but is said to be O (NLOGM ), it can be said that the character set frequently fixed problem is an O (n) algorithm.

#pragma warning (disable:4996) #include <iostream> #include <cstring> #include <string> #include < vector> #include <cstdio> #include <algorithm>using namespace std; #define MAXN 210000#define ll Long Longstruct palindrometree{/*static variables*/const static int maxn = 210000;const static int ch = 26;/* * Nxt[i][ch] Node  I after receiving the character Ch point of the node * Fail[i] At the end of a palindrome string of the node I refers to the end of the last palindrome string fail[ababa] = ABA Fail[aba] = a * cnt[i] node I represents the number of palindrome string * Num[i] The number of palindrome string ending with the end character of the palindrome indicated by node I * len[i] node I represents the length of the palindrome string * S[i] the first character * Last newly added character the longest palindrome produced The total number of nodes * p nodes * n the length of the current string */int nxt[maxn][ch];int fail[maxn];int cnt[maxn];int num[maxn];int len[ Maxn];int s[maxn];int last;int n;int p;int newnode (int length) {memset (nxt[p], 0, sizeof (nxt[p])); Cnt[p] = num[p] = 0;len[p ] = Length;return p++;} void Init () {p = 0;newnode (0); NewNode ( -1); last = 0;n = 0; S[n] = -1;fail[0] = 1;} int getfail (int x) {while (S[n-len[x]-1]! = S[n]) x = FAIL[x];return x;} void Add (int c) {c-= ' a '; S[++n] = C;int cur = getfail (last), if (!nxt[cur][c]) {int now = NewNode (Len[cur] + 2); Fail[now] = Nxt[getfail (Fail[cur]) [ C];NXT[CUR][C] = Now;num[now] = Num[fail[now]] + 1;} Last = Nxt[cur][c];cnt[last] + +;} void Count () {for (int i = p-1; I >= 0; i) {Cnt[fail[i]] + = Cnt[i];}}; Palindrometree Treex, Treey;char BUF1[MAXN], buf2[maxn];ll ans;void dfs (int u, int v) {for (int i = 0; i < treex.ch; ++i {int x = Treex.nxt[u][i];int y = treey.nxt[v][i];if (x&&y) {ans + = (ll) treex.cnt[x] * Treey.cnt[y];d fs (x, y);}} int main () {int T; cin >> T; int ca = 0;while (t--) {treex.init (); Treey.init (); scanf ("%s%s", Buf1, buf2); int len1 = St Rlen (BUF1), len2 = strlen (BUF2), for (int i = 0; i < len1; ++i) {Treex.add (buf1[i]);} for (int i = 0; i < len2; ++i) {Treey.add (buf2[i]);} Treex.count (); Treey.count (); ans = 0;dfs (0, 0);d FS (1, 1);p rintf ("Case #%d:%i64d\n", ++ca, ans);} return 0;}

The problem to Slow (palindromic Tree)

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.