Zoj 3296 Mancher Algorithm + minimum range coverage

Source: Internet
Author: User

 


Here is a string that you can splice at least several times into a string. You can splice only two ECHO strings at a time, which can overlap.

Idea: First, find the intervals represented by all the longest reply substrings with each vertex as the symmetric axis. In this case, consider whether the reply strings are odd or even, however, the Mancher algorithm solves this problem well ....

The next step is to select the least range to overwrite the entire range, and then there is a naked range coverage problem. Use a greedy one to maintain the farthest distance currently covered by now_end, then the line segment to be selected should be left endpoint on the left of now_end, right endpoint on the Right of now_end, and extend to the right as far as possible ....

Mancher learning:

P [I] indicates the return radius centered on I,
P [I]-1 is exactly the length of the first input string of the original string.
Drawing and drawing will know, because the two sides must match the character g
Main Mancher algorithm.
Address: http://blog.csdn.net/ggggiqnypgjg/article/details/6645824
Function: Obtain the I-centered return radius p [I].
Parameter: input the constructed string length.
Special note: because an invalid character is added before, the subscript starts from 1.
 

 

Code:


[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
Using namespace std;
Const int maxn = 100010;
Namespace M {
Int n;
Struct node {
Int a, B;
Node (){}
Node (int _ a, int _ B): a (_ a), B (_ B ){};
Bool operator <(const node & cmp) const {
Return a <cmp.;
}
} In [50010];
Void solve ()
{
Int ter = 0;
For (int I = 0; I <n; I ++) ter = max (ter, in [I]. B );
Sort (in, in + n );
Int ans = 0, pt = 0, now_end = in [0].;
While (true)
{
If (now_end> ter) break;
Int mx =-1;
While (pt <n)
{
If (in [pt]. a <= now_end)
{
If (in [pt]. B> mx) mx = in [pt]. B;
Pt ++;
}
Else
{
Break;
}
}
Now_end = mx + 1;
Ans ++;
}
Printf ("% d \ n", ans-1 );
}
}
Struct Mancher {
Char str [maxn]; // start from index 1
Int p [maxn];
Char s [maxn];
Int n;
Void checkmax (int & ans, int B ){
If (B> ans) ans = B;
}
Inline int min (int a, int B ){
Return a <B? A: B;
}
Void kp (){
Int I;
Int mx = 0;
Int id;
For (I = 1; I <n; I ++ ){
If (mx> I)
P [I] = min (p [2 * id-I], p [id] + id-I );
Else
P [I] = 1;
For (; str [I + p [I] = str [I-p [I]; p [I] ++ );
If (p [I] + I> mx ){
Mx = p [I] + I;
Id = I;
}
}
}
Void pre ()
{
Int I, j, k;
N = strlen (s );
Str [0] = '$ ';
Str [1] = '#';
For (I = 0; I <n; I ++)
{
Str [I * 2 + 2] = s [I];
Str [I * 2 + 3] = '#';
}
N = n * 2 + 2;
Str [n] = 0;
}
Void solve () // obtain the range of all the longest response strings
{
Int & tot = M: n;
Tot = 0;
For (int I = 2; I <n; I ++)
{
If (I % 2 & p [I] = 1) continue;
If (I % 2)
{
M: in [tot ++] = M: node (I/2-p [I]/2 + 1, I/2 + p [I]/2 );
// Printf ("% d \ n", I/2-p [I]/2 + 1, I/2 + p [I]/2 );
}
Else
{
M: in [tot ++] = M: node (I/2-(p [I]/2-1 ), i/2 + (p [I]/2-1 ));
// Printf ("% d \ n", I/2-(p [I]/2-1 ), i/2 + (p [I]/2-1 ));
}
}
}
} Task1;
Int main ()
{
While (scanf ("% s", task1.s )! = EOF)
{
Task1.pre ();
Task1.kp ();
Task1.solve ();
M: solve ();
}
Return 0;
}

# Include <cstdio>
# Include <cstring>
# Include <algorithm>
Using namespace std;
Const int maxn = 100010;
Namespace M {
Int n;
Struct node {
Int a, B;
Node (){}
Node (int _ a, int _ B): a (_ a), B (_ B ){};
Bool operator <(const node & cmp) const {
Return a <cmp.;
}
} In [50010];
Void solve ()
{
Int ter = 0;
For (int I = 0; I <n; I ++) ter = max (ter, in [I]. B );
Sort (in, in + n );
Int ans = 0, pt = 0, now_end = in [0].;
While (true)
{
If (now_end> ter) break;
Int mx =-1;
While (pt <n)
{
If (in [pt]. a <= now_end)
{
If (in [pt]. B> mx) mx = in [pt]. B;
Pt ++;
}
Else
{
Break;
}
}
Now_end = mx + 1;
Ans ++;
}
Printf ("% d \ n", ans-1 );
}
}
Struct Mancher {
Char str [maxn]; // start from index 1
Int p [maxn];
Char s [maxn];
Int n;
Void checkmax (int & ans, int B ){
If (B> ans) ans = B;
}
Inline int min (int a, int B ){
Return a <B? A: B;
}
Void kp (){
Int I;
Int mx = 0;
Int id;
For (I = 1; I <n; I ++ ){
If (mx> I)
P [I] = min (p [2 * id-I], p [id] + id-I );
Else
P [I] = 1;
For (; str [I + p [I] = str [I-p [I]; p [I] ++ );
If (p [I] + I> mx ){
Mx = p [I] + I;
Id = I;
}
}
}
Void pre ()
{
Int I, j, k;
N = strlen (s );
Str [0] = '$ ';
Str [1] = '#';
For (I = 0; I <n; I ++)
{
Str [I * 2 + 2] = s [I];
Str [I * 2 + 3] = '#';
}
N = n * 2 + 2;
Str [n] = 0;
}
Void solve () // obtain the range of all the longest response strings
{
Int & tot = M: n;
Tot = 0;
For (int I = 2; I <n; I ++)
{
If (I % 2 & p [I] = 1) continue;
If (I % 2)
{
M: in [tot ++] = M: node (I/2-p [I]/2 + 1, I/2 + p [I]/2 );
// Printf ("% d \ n", I/2-p [I]/2 + 1, I/2 + p [I]/2 );
}
Else
{
M: in [tot ++] = M: node (I/2-(p [I]/2-1 ), i/2 + (p [I]/2-1 ));
// Printf ("% d \ n", I/2-(p [I]/2-1 ), i/2 + (p [I]/2-1 ));
}
}
}
} Task1;
Int main ()
{
While (scanf ("% s", task1.s )! = EOF)
{
Task1.pre ();
Task1.kp ();
Task1.solve ();
M: solve ();
}
Return 0;
}

 

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.