UVA 12378 Ball Blasting Game 【Manacher回文串】

Ball Blasting Game

Morteza is playing a ball blasting game. In this game there is a chain of different colored balls. He is expected to explode as many balls as possible by aligning same-colored balls and making a sequence of them. To align balls, he can aim and shoot a new ball into a position in the chain, thus adding it there. If the new ball makes a sequence of two or more same-colored balls with its immediate neighbors, then the sequence blows up breaking the chain up into two parts. The two sections draw together to reform a single chain. Again, if the colors of the newly aligned balls (on joining ends of the two sections) match, the entire run of the suit explodes. New explosions ensue as long as joining sections bear new matches.

For instance, consider a chain symbolized in the string "GGGBWWRRWRR", with each letter representing the color of the corresponding ball in the chain. The train of balls therefore is composed of 6 sequences in 4 colors. Here, a ball can be added in 12 different positions. Shooting a red ball between the two middle red balls triggers two successive explosions which leave the chain "GGGBRR".

Morteza reaches a challenge stage in which he has only one ball to shoot but the color of which he can choose. He may not advance to the next stage unless he takes a shot that sets off the largest possible number of explosions. He has to replay all through this repetitive stage should he fail and needs your help in finding the largest possible number of successive explosions to save him a great deal of suffering.

Input

The first line of input contains an integer T≤100 denoting the number of test-cases. Each test-case is represented by a single line containing a string of upper case letters ('A'-'Z') of size of at most 100,000.

Output

For each test-case, output on a single line the maximum possible number of explosions.

Sample Input

Sample Output

3

GGGBWWRRWRR

XAABCBAXAAAA

CCC

2

4

1


【题目大意】

给定一个字符串,向某个位置插入一个字符C使得两个或多个字符C相邻,然后消除这些字符,消除后得到左右两个新字符串,连接两个新字符串,如果连接处是两个或多个相同的字符,则同样消除掉,以此类推

【题解】

把给定的字符串进行预处理,相邻多个相同的字符合并成一个字符

如GGGBWWRRWRR合并成GBWRWR

XAABCBAXAAA合并成XABCBAXA

然后就转化成求最长回文子串问题,

那么怎么求最长回文子串?o(n^2)的算法显然不行,那么需要小于n^2的算法

百度得到Manacher算法

这篇博文讲的不错

http://blog.163.com/zhaohai_1988/blog/static/2095100852012716105847112/

本题代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#define LEN 1000000
using namespace std;
int len,p[LEN],ans;
char s[LEN],GivenStr[LEN];

void init()
{
    memset(s,0,sizeof(s));
    memset(p,0,sizeof(p));
    s[0]='$';
    s[1]='#';
    len=1;
    ans=0;

    getchar();
    scanf("%s",GivenStr);
    int lenGiven=strlen(GivenStr);
    for (int i=0; i<lenGiven; i++)
    {
        if (GivenStr[i]==GivenStr[i+1]) continue;
        s[++len]=GivenStr[i];
        s[++len]='#';
    }
}

void LPS()
{
    int id=1,mx=0;
    for (int i=1; i<len; i++)
    {
        if (mx>i)
            p[i]=min(p[2*id-i],mx-i);
        else
            p[i]=1;

        while (s[i+p[i]]==s[i-p[i]]) p[i]++;

        if (p[i]+i>mx)
        {
            mx=p[i]+i;
            id=i;
        }
        ans=max(ans,p[i]);
    }
}

void out()
{
    printf("%d
",ans/2);
}

int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        init();
        LPS();//Longest Palindromic Substring
        out();
    }
}

=================

 【转载注明出处

    这个算法有一个很巧妙的地方,它把奇数的回文串和偶数的回文串统一起来考虑了。这一点一直是在做回文串问题中时比较烦的地方。这个算法还有一个很好的地方就是充分利用了字符匹配的特殊性,避免了大量不必要的重复匹配。

    算法大致过程是这样。先在每两个相邻字符中间插入一个分隔符,当然这个分隔符要在原串中没有出现过。一般可以用‘#’分隔。这样就非常巧妙的将奇数长度回文串与偶数长度回文串统一起来考虑了(见下面的一个例子,回文串长度全为奇数了),然后用一个辅助数组P记录以每个字符为中心的最长回文串的信息。P[id]记录的是以字符str[id]为中心的最长回文串,当以str[id]为第一个字符,这个最长回文串向右延伸了P[id]个字符。
    原串:       w     a    a     b     w    s   w      f     d
    新串:     #  w  # a # a #  b  # w # s # w #  f  # d #
辅助数组P: 1  2   1 2 3 2 1   2  1  2 1 4 1  2 1  2  1 2 1

这里有一个很好的性质,P[id]-1就是该回文子串在原串中的长度(包括‘#’)。如果这里不是特别清楚,可以自己拿出纸来画一画,自己体会体会。当然这里可能每个人写法不尽相同,不过我想大致思路应该是一样的吧。

现在的关键问题就在于怎么在O(n)时间复杂度内求出P数组了。只要把这个P数组求出来,最长回文子串就可以直接扫一遍得出来了。

    那么怎么计算P[i]呢?该算法增加两个辅助变量(其实一个就够了,两个更清晰)id和mx,其中id表示最大回文子串中心的位置,mx则为id+P[id],也就是最大回文子串的边界。

    然后可以得到一个非常神奇的结论,这个算法的关键点就在这里了:如果mx > i,那么

P[i] >= MIN(P[2 * id - i], mx - i)。就是这个串卡了我非常久。实际上如果把它写得复杂一点,理解起来会简单很多:

//记j = 2 * id - i,也就是说 j 是 i 关于 id 的对称点。
if (mx - i > P[j]) 
    P[i] = P[j];
else /* P[j] >= mx - i */
    P[i] = mx - i; // P[i] >= mx - i,取最小值,之后再匹配更新。

当 mx - i > P[j] 的时候,以S[j]为中心的回文子串包含在以S[id]为中心的回文子串中,由于 i 和 j 对称,以S[i]为中心的回文子串必然包含在以S[id]为中心的回文子串中,所以必有 P[i] = P[j]。

#include<vector>
#include<iostream>
using namespace std;

const int N=300010;
int n, p[N];
char s[N], str[N];

#define _min(x, y) ((x)<(y)?(x):(y))

void kp()
{
    int i;
    int mx = 0;
    int id;
    for(i=n; str[i]!=0; i++)//清除n后边多余的部分
        str[i] = 0; //没有这一句有问题。。就过不了ural1297,比如数据:ababa aba
    for(i=1; i<n; i++)
    {
        if( mx > i )
            p[i] = _min( p[2*id-i], p[id]+id-i );
        //因为是从左往右扫描的这里i>id, 2*id-i是i关于id的对称点,该对称点在id的左端
        //p[id]+id是描述中的mx,即id向右延伸的端点位置
        //显然向右延伸是可能超出mx的,所以要有下边的for循环
        else
            p[i] = 1;
        for(; str[i+p[i]] == str[i-p[i]]; p[i]++);

        if( p[i] + i > mx )//更新mx与id,因为mx是向右延伸的最大长度,所以实时更新
        {
            mx = p[i] + i;
            id = i;
        }
    }
}

void init()//处理字符串
{
    int i, j, k;
    str[0] = '$';
    str[1] = '#';
    for(i=0; i<n; i++)
    {
        str[i*2+2] = s[i];
        str[i*2+3] = '#';
    }
    n = n*2+2;
    s[n] = 0;
}

int main()
{
    int i, ans;
    while(scanf("%s", s)!=EOF)
    {
        n = strlen(s);//n为给定字符串s的长度
        init();
        kp();
        ans = 0;
        for(i=0; i<n; i++)
            if(p[i]>ans)//寻找最大的长度ans
                ans = p[i];
        printf("%d
", ans-1);
    }
    return 0;
}


当 P[j] > mx - i 的时候,以S[j]为中心的回文子串不完全包含于以S[id]为中心的回文子串中,但是基于对称性可知,也就是说以S[i]为中心的回文子串,其向右至少会扩张到mx的位置,也就是说 P[i] >= mx - i。至于mx之后的部分是否对称,就只能老老实实去匹配了。

由于这个算法是线性从前往后扫的。那么当我们准备求P[i]的时候,i以前的P[j]我们是已经得到了的。我们用mx记在i之前的回文串中,延伸至最右端的位置。同时用id这个变量记下取得这个最优mx时的id值。(注:为了防止字符比较的时候越界,我在这个加了‘#’的字符串之前还加了另一个特殊字符‘$’,故我的新串下标是从1开始的)

if( mx > i)

p[i]=MIN( p[2*id-i], mx-i);

就是当前面比较的最远长度mx>i的时候,P[i]有一个最小值。这个算法的核心思想就在这里,为什么P数组满足这样一个性质呢?

(下面的部分为图片形式)

LEETCODE上也有这个题的详细说明,不过是英文版本的。

原文地址:https://www.cnblogs.com/zhyfzy/p/4021322.html