Palindrome Tree (count all palindrome strings)-Mccme 1750подпалиндромы

Source: Internet
Author: User

подпалиндромы
problem ' s Link: http://informatics.mccme.ru//mod/statements/view.php?chapterid=1750#

 

Mean:

Give you a string of not more than 1e5 length, to count the total number of palindrome string. (The first Russian question, fortunately there is Google translation).

Analyse:

If the topic is said to be different palindrome string, the problem is a bare palindrome tree.

Because of the number of palindrome required for all substrings, we also need to use Suffixlink to go up the statistical answer when inserting.

Time complexity:o (N)

 

Source code:

/*
* This code was made by Crazyacking
* verdict:accepted
* Submission date:2015-08-17-19.13
* time:0ms
* memory:137kb
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL Long Long
#define ULL unsigned long long
using namespace STD;

Const int MAXN = 105000;
struct node
{
int Next[ -];
int Len;
int Sufflink;
int Num;
};
int Len;
CharS[MAXN];
node Tree[MAXN];
int Num;//Node 1-root with len-1, node 2-root with Len 0
int Suff;//MAX suffix palindrome
Long Long ans;
BOOL Addletter(int POS)
{
int cur = Suff, Curlen = 0;
int Let =S[POS] - ' A ';
while(true)
{
Curlen = Tree[cur].Len;
if(POS-1-Curlen>=0&&S[POS-1-Curlen]==S[POS]) Break;
cur = Tree[cur].Sufflink;
}
if(Tree[cur].Next[ Let])
{
Suff = Tree[cur].Next[ Let];
return false;
} Suff = ++Num;
Tree[Num].Len = Tree[cur].Len + 2;
Tree[cur].Next[ Let] = Num;
if(Tree[Num].Len == 1)
{
Tree[Num].Sufflink = 2;
Tree[Num].Num = 1;
return true;
}
while(true)
{
cur = Tree[cur].Sufflink;
Curlen = Tree[cur].Len;
if(POS - 1 - Curlen >= 0 &&S[POS - 1 - Curlen] ==S[POS])
{
Tree[Num].Sufflink = Tree[cur].Next[ Let];
Break;
}
}
Tree[Num].Num = 1 + Tree[Tree[Num].Sufflink].Num;
return true;
}
void Inittree()
{
Num = 2;Suff = 2;
Tree[1].Len = -1;Tree[1].Sufflink = 1;
Tree[2].Len = 0;Tree[2].Sufflink = 1;
}
int Main()
{
gets(s);
Inittree();
for(int I = 0; s[I]; I++)
{
Addletter(I);
ans += Tree[Suff].Num;
} cout << ans << Endl;
return 0;
}

Palindrome Tree (count all palindrome strings)-Mccme 1750подпалиндромы

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.