Clickomania

Time Limit: 10000ms
Memory Limit: 32768KB

64-bit integer IO format: %I64d      Java class name: Main

Clickomania is a puzzle in which one starts with a rectangular grid of cells of different colours. In each step, a player selects ("clicks") a cell. All connected cells of the same colour as the selected cell (including itself) are removed if the selected cell is connected to at least one other cell of the same colour. The resulting "hole" is filled in by adjacent cells based on some rule, and the object of the game is to remove all cells in the grid. In this problem, we are interested in the one-dimensional version of the problem. The starting point of the puzzle is a string of colours (each represented by an uppercase letter).
At any point, one may select (click) a letter provided that the same letter occurs before or after the one selected. The substring of the same letter containing the selected letter is removed, and the string is shortened to remove the hole created. To solve the puzzle, the player has to remove all letters and obtain the empty string. If the player obtains a non-empty string in which no letter can be selected, then the player loses. For example, if one starts with the string "ABBAABBAAB", selecting the first "B" gives "AAABBAAB". Next, selecting the last "A" gives "AAABBB". Selecting an "A" followed by a "B" gives the empty string. On the other hand, if one selects the third "B" first, the string "ABBAAAAB" is obtained. One may verify that regardless of the next selections, we obtain either the string "A" or the string "B" in which no letter can be selected. Thus, one must be careful in the sequence of selections chosen in order to solve a puzzle. Furthermore,
there are some puzzles that cannot be solved regardless of the choice of selections. For example, "ABBAAAAB" is not a solvable puzzle. Some facts are known about solvable puzzles: The empty string is solvable. If x and y are solvable puzzles, so are xy, AxA, and AxAyA for any uppercase letter
A. All other puzzles not covered by the rules above are unsolvable.
Given a puzzle, your task is to determine whether it can be solved or not.

 

Input

Each case of input is specified by a single line. Each line contains a string of uppercase letters. Each string has at least one but no more than 150 characters. The input is terminated by the end of file.

 

Output

For each input case, print solvable on a single line if there is a sequence of selections that solves the puzzle. Otherwise, print unsolvable on a line.

 

Sample Input

ABBAABBAAB
ABBAAAAB

Sample Output

solvable
unsolvable 解题思路:因为题目中给出了状态转移的三种情况。即xy, AxA, and AxAyA。所以对这些情况分别讨论。最不好做的就是AxAyA这种情况。找到中间的A之后,判断中间的A分别和两边的A之间是否可消,这里将空串处理为可消的情况。
#include<bits/stdc++.h>
using namespace std;
const int maxn=300;
bool dp[maxn][maxn];
bool jud(int x,int y,char s[]){
for(int i=x+1;i<y;i++){
if(dp[x][i]&&dp[i+1][y]){ //AABBB即xy的情况
return true;
}
}
if(s[x]==s[y]){
if(y-x==1){ //AA即x的情况
return true;
}
if(dp[x+1][y-1]){ //ABBA即AxA情况
return true;
}
for(int i=x+1;i<y;i++){ //ABBACCA即AxAyA情况
if(s[i]==s[x]){ //枚举中间的A
//如果A的左侧有空串或能消并且A的右侧有空串或能消,即能消
if((i-1<x+1||dp[x+1][i-1])&&(i+1>y-1||dp[i+1][y-1])){
return true;
}
}
}
}
return false;
}
void DP(char s[]){
int len=strlen(s);
for(int k=1;k<len;k++){
for(int i=0;i<len-k;i++){
int j=i+k;
dp[i][j]=jud(i,j,s);
}
}
}
int main(){
char str[300];
while(scanf("%s",str)!=EOF){
memset(dp,0,sizeof(dp));
DP(str);
int len=strlen(str);
if(!dp[0][len-1]){
printf("un");
}
printf("solvable\n");
}
return 0;
}

  

 

BNU7538——Clickomania——————【区间dp】的更多相关文章

  1. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  2. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  3. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  4. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  5. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

  6. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  7. BZOJ 1260&UVa 4394 区间DP

    题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...

  8. 区间dp总结篇

    前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...

  9. Uva 10891 经典博弈区间DP

    经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...

  10. 2016 年沈阳网络赛---QSC and Master(区间DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5900 Problem Description Every school has some legend ...

随机推荐

  1. bitbucket使用教程

    Bitbucket使用说明: 使用者请直接看第一步,第二步和egit使用说明, 需要自己创建仓库的可以看三四步 第一步:新用户注册 bitbucket.org 然后按步骤创建一个教程代码库 可以选择下 ...

  2. Dapper 简单封装

    using System; using System.Collections.Generic; using System.Text; using Dapper; using System.Data; ...

  3. firefox 59 无法使用 pac 代理上网

    最近装了 firefox,电脑配置不太高,chrome 太吃内存了. 但是发现 SwitchyOmega的 pac 模式无法工作,这篇文章提到了两个思路, 其中network.dns.disableI ...

  4. 函数返回值string与返回值bool区别------c++程序设计原理与实践(进阶篇)

    为什么find_from_addr()和find_subject()如此不同?比如,find_from_addr()返回bool值,而find_subject()返回string.原因在于我们想说明: ...

  5. 一大波趣图:CSS的力量

    CSS的力量         CSS的作用,一目了然~     见识一下CSS的厉害!   用了CSS,效果显著   HTML5 + CSS3 + Javascript会怎么样?       HTML ...

  6. HDP 中 yarn 和 MR2 的配置

    以下说明均以集群中 slave 结点的配置为 48G内存,12块硬盘,12核(core) CPU 为例. 在 Yarn 中,一个 Container 是一个基础的包含内存和CPU 的单元.为了较好的平 ...

  7. 题解 P1876 【开灯】

    题目链接 编者说得对 一道很明显的数学题,相信大家小学都做过. 通俗一点,就是找因数为奇数个的数.而这一类的数.明显的是平方数. 所以就是找n以内的平方数. 废话少说,直接上题解. #include& ...

  8. 181114socke编程

    一.Socket Families 地址簇 socket.AF_UNIX socket.AF_INET socket.AF_INET6 二.Socker Types socket.SOCK_STREA ...

  9. 查看 tensorflow 是GPU版本 还是CPU版本

    在Python环境中输入: import os from tensorflow.python.client import device_lib os.environ["TF_CPP_MIN_ ...

  10. WPF优化:Freezable冻结对象

    原文:WPF优化:Freezable冻结对象 WPF虽然很美观,效果很炫,但是对资源的消耗也很大,尤其是初次接触WPF的人,因为很多地方虽然实现了想要的效果,但是由于经验问题,所以也会造成很大的资源浪 ...