"Noip Simulation" "Messing with AC" "artifice" "Multiplication principle" palindrome string count

Source: Internet
Author: User

Palindrome string count (calc.pas/calc.c/calc.cpp) "Title description"

Although a science student, MCX often claims to be a true liberal arts student. Somehow, he had a strange love for reciting, which prompted him to go to a race of creatures known for his memory. However, he soon found that this did not satisfy his love of reciting heart, but as a powerful Boer, he found such a self-abuse of the way-recite the gene sequence. But this is really too abusive, even Mcx also a little overwhelmed. But he found that if he knew beforehand how many pairs of disjoint palindrome in the sequence, he might find a way to remember. To further verify this method,Mcx decided to experiment by selecting a string SS consisting of lowercase letters . However, because the non-intersecting palindrome string is too much, he quickly counted dizzy. But he believes that the problem in front of you is just a piece of cake.

"Noun explanation"

1. for the string SS, set its length to Len, then the following uses Si to denote the SS I - character (1<=i<=len)

2.s[i,j] represents a string of SS,s[i,j] = "sisi+1si+2 ... SJ-2SJ-1SJ ", such as when the SS is " abcgfd ",

s[2,5] = "BCGF", s[1,5] = "ABCGF"

3. when a string is called a palindrome, and only if the string is reversed and is the same as the original string, such as "ABCBA"

4. consider a four-tuple (l,r,l,r), when S[l,r] and S[l,r] are palindrome strings, and meet 1 <= l <=r< l <= R <= Len , we call S[l,r] and S[l,r] For a pair of disjoint palindrome strings. This is the number of the four-tuple. Two four tuples are the same if and only if the corresponding l,r,l,r are the same.

"topic input"

only one row, which is a string SS, guaranteed to consist of all lowercase letters, ending with newline character flags.

"topic Output"

A single row, an integer representing the logarithm of disjoint palindrome strings.

"Sample Input"

Aaa

"Sample Output"

5

"Sample Interpretation"

SS = "AAA", any string of SS is a palindrome string, in total there are 5 pairs of disjoint palindrome string:

(1,1,2,2), (1,1,2,3), (1,1,3,3), (1,2,3,3), (2,2,3,3). ( this is represented by the four-tuple in the noun interpretation )

"Data Range"

the 50% data meets the SS length of not more than

the 100% data satisfies the SS length not exceeding

"Solution"

First spit out the data of this problem, O (N3) of the large violence over 90% points .... Of course, the following is the AC approach.

First use interval DP to find the ch[i] between Ch[j] is not a palindrome string, the transfer equation is pldr[i][j]=pldr[i+1][j-1], pay attention to i==j and j-i+1==2 case. N2 then find out how many palindrome strings are in front of each character (including himself), and then use N2 to find the number of palindrome strings that start with the character next to the character after each character. Finally n the sum of the number before each point multiplied by the number (multiplication principle), which is the answer.

AC Code:

1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5 Charch[ .];6 BOOLpldr[ .][ .];7 intLenn;8 intbef[ .],aft[ .];9 Long Longans;Ten intMain () { Onescanf"%s", ch+1); Lenn=strlen (ch+1); A      for(intI=1; i<=lenn;++i) pldr[i][i]=true; -      for(inti=lenn;i>=1;--i) -          for(intj=i;j<=lenn;++j) the             if(j>i&&ch[i]==Ch[j]) { -                 if(I==J) pldr[i][j]=true; -                 Else if(j-i+1==2) pldr[i][j]=true; -                 Elsepldr[i][j]=pldr[i+1][j-1]; +             } -      for(intI=1; i<=lenn;++i) { +bef[i]=bef[i-1]; A          for(intj=1; j<=i;++j) at             if(Pldr[j][i]) + +Bef[i]; -     } -      for(intI=1; i<lenn;++i) -          for(intj=i;j<=lenn;++j) -             if(pldr[i+1][J]) + +Aft[i]; -      for(intI=1; i<=lenn;++i) ans+=bef[i]*Aft[i]; inprintf"%i64d", ans); -     return 0; to}

"Noip Simulation" "Messing with AC" "artifice" "Multiplication principle" palindrome string count

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.