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. mysql 复制(主从复制)

    一.概述 让一台服务器的数据与其他服务器数据保持同步.一台主库的数据可以同步到多台备库上,而备库本身也可以配置成其他服务器的主库. 主要应用: 1) 数据分布 2) 负载均衡 3) 伪备份.在备份基础 ...

  2. BumpMap、NormalMap的区别

    原文:http://linuxtest.blog.163.com/blog/static/199927088201275102145354/   一种是Emboss Bump Map(浮雕凹凸贴图), ...

  3. NSTimeZone时区

    前言 NSTimeZone 表示时区信息. 1.NSTimeZone 时区的创建 NSTimeZone *zone1 = [[NSTimeZone alloc] init]; // 根据时区名称创建 ...

  4. 数据结构之BF算法,kmp算法,三元组,十字链表总结

    在这一章中,老师教了我们四种数据结构:BF算法,kmp算法,三元组和十字链表:还给我们讲了2019年团体天体赛中T1-8的AI题 1.对于BF和kmp算法,老师除了在课堂上讲解算法的主要核心思想外,还 ...

  5. 题解 P2863 【[USACO06JAN]牛的舞会The Cow Prom】

    题目链接 赤裸裸的板子,就加一个特判就行.直接上代码 #include<stdio.h> #include<algorithm> #include<iostream> ...

  6. easyui打开dialog后给弹出框内输入框赋值问题

    在写一个弹出页面的时候,里面有一些输入框,需要在弹出的时候从数据库取值并且赋值,刚开始在弹出的时候使用$(id).val(value),结果赋值失败,为空当时纠结了一会,然后突然想到在easyui打开 ...

  7. Phpstudy+WordPress安装详解

    Phpstudy+WordPress安装详解 1.安装phpStudy程序 将下载的phpStudy程序解压到某个分区的根目录中,第一次使用会提示你初始化一下: 之后在主控制界面点击启动即可. php ...

  8. 利用PHP 简单实现加减法验证码

    <?php header('Content-Type: image/png'); $im = imagecreatetruecolor( 200 , 50 );//生成图片长宽 // Creat ...

  9. 用python脚本 从xls文件中读取数据

    导入 xlrd 第三方模块 import xlrd data = xlrd.open_workbook('test.xlsx') # 打开xls文件 table = data.sheets()[0] ...

  10. 【算法笔记】B1009 说反话

    1009 说反话 (20 分) 给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式: 测试输入包含一个测试用例,在一行内给出总长度不超过 80 的字符串.字符串由若干单词和若干空格 ...