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,,sizeof(s));
memset(p,,sizeof(p));
s[]='$';
s[]='#';
len=;
ans=; getchar();
scanf("%s",GivenStr);
int lenGiven=strlen(GivenStr);
for (int i=; i<lenGiven; i++)
{
if (GivenStr[i]==GivenStr[i+]) continue;
s[++len]=GivenStr[i];
s[++len]='#';
}
} void LPS()
{
int id=,mx=;
for (int i=; i<len; i++)
{
if (mx>i)
p[i]=min(p[*id-i],mx-i);
else
p[i]=; 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\n",ans/);
} 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=;
int n, p[N];
char s[N], str[N]; #define _min(x, y) ((x)<(y)?(x):(y)) void kp()
{
int i;
int mx = ;
int id;
for(i=n; str[i]!=; i++)//清除n后边多余的部分
str[i] = ; //没有这一句有问题。。就过不了ural1297,比如数据:ababa aba
for(i=; i<n; i++)
{
if( mx > i )
p[i] = _min( p[*id-i], p[id]+id-i );
//因为是从左往右扫描的这里i>id, 2*id-i是i关于id的对称点,该对称点在id的左端
//p[id]+id是描述中的mx,即id向右延伸的端点位置
//显然向右延伸是可能超出mx的,所以要有下边的for循环
else
p[i] = ;
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[] = '$';
str[] = '#';
for(i=; i<n; i++)
{
str[i*+] = s[i];
str[i*+] = '#';
}
n = n*+;
s[n] = ;
} int main()
{
int i, ans;
while(scanf("%s", s)!=EOF)
{
n = strlen(s);//n为给定字符串s的长度
init();
kp();
ans = ;
for(i=; i<n; i++)
if(p[i]>ans)//寻找最大的长度ans
ans = p[i];
printf("%d\n", ans-);
}
return ;
}
当 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上也有这个题的详细说明,不过是英文版本的。
UVA 12378 Ball Blasting Game 【Manacher回文串】的更多相关文章
- UVA 11584 Partitioning by Palindromes 划分回文串 (Manacher算法)
d[i]表示前面i个字符划分成的最小回文串个数, 转移:当第i字符加进来和前面区间j构成回文串,那么d[i] = d[j]+1. 要判断前面的字符j+1到i是不是回文串,可以用Manacher算法预处 ...
- 2015 UESTC Training for Search Algorithm & String - M - Palindromic String【Manacher回文串】
O(n)的复杂度求回文串:Manacher算法 定义一个回文值,字符串S是K重回文串,当且仅当S是回文串,且其长度为⌊N/2⌋的前缀和长度为⌊N/2⌋的后缀是K−1重回文串 现在给一个2*10^6长度 ...
- HDU3068 最长回文 MANACHER+回文串
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符 ...
- Best Reward---hdu3613(manacher 回文串)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题意就是给你一个串s 然后求把s分成两部分之后的价值总和是多少,分开的串 如果是回文那么价值就是 ...
- Manacher回文串算法学习记录
FROM: http://hi.baidu.com/chenwenwen0210/item/482c84396476f0e02f8ec230 #include<stdio.h> #inc ...
- 【模板】Manacher 回文串
推荐两个讲得很好的博客: http://blog.sina.com.cn/s/blog_70811e1a01014esn.html https://segmentfault.com/a/1190000 ...
- BZOJ 2342 回文串-Manacher
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2342 思路:先跑一遍Manacher求出p[i]为每个位置为中心的回文半径,因为双倍回文串 ...
- BZOJ 2565 回文串-Manacher
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2565 题意:中文题 思路:定义L[i],R[i].表示以i为左端点/右端点时,最长回文串长 ...
- POJ 3974 回文串-Manacher
题目链接:http://poj.org/problem?id=3974 题意:求出给定字符串的最长回文串长度. 思路:裸的Manacher模板题. #include<iostream> # ...
随机推荐
- js 得到当前季度
Date.prototype.getQuarter = function() { var month = this.getMonth(); if(month < 3) { return '第一季 ...
- Thinkphp 表单验证
在服务器端通过tp框架实现表单验证 用户名.密码.重复密码.邮箱.qq.手机号码.爱好.学历 具体步骤: 制作表单 表单form数据通过create()方法收集(验证功能要求我们必须通过create( ...
- CSS hack技巧
CSS hack技巧一览,原文来自CSDN freshlover的博客专栏<史上最全CSS Hack方式一览> 什么是CSS hack 由于不同厂商的流览器或某浏览器的不同版本(如IE6- ...
- php 面向对象编程实例 __construct 和 __destruct 区别
大多数类都有一种称为构造函数的特殊方法.当创建一个对象时,它将自动调用构造函数,也就是使用new这个关键字来实例化对象的时候自动调用构造方 法.构 造函数的声明与其它操作的声明一样,只是其名称必须是_ ...
- Boost使用笔记(Smart_ptr)
我是Word写的,复制过来实在懒得在排版了,有兴趣的朋友可以去我的百度文库看,谢谢 http://wenku.baidu.com/view/34e485e2f61fb7360b4c653e.html ...
- Scut:缓存管理
Scut 的缓存管理看起来还是蛮复杂的. redis 本身就有内存缓存+持久化的作用,Scut还是自己封装了一层内存缓存+Redis缓存+持久化. . 这是一个缩略版本的结构图. 1. 上半部分是 ...
- Hybrid----优秀开源代码解读之JS与iOS Native Code互调的优雅实现方案-备
本篇为大家介绍一个优秀的开源小项目:WebViewJavascriptBridge. 它优雅地实现了在使用UIWebView时JS与ios 的ObjC nativecode之间的互调,支持消息发送.接 ...
- mac 下 配置 阿帕奇
1.从 tomcat 官网(http://tomcat.apache.org/download-90.cgi)下载 完整的 tomcat包. 2.将红框中的包下载完,然后解压到任意一个目录,将其命名为 ...
- Mifare 0简介
Mifare UltraLight又称为MF0,从UltraLight(超轻的)这个名字就可以看出来,它是一个低成本.小容量的卡片.低成本,是指它是目前市场中价格最低的遵守ISO14443A协议的芯片 ...
- Remember the Word
uvalive3942:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&pag ...