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. 历届试题 小数第n位

    问题描述 我们知道,整数做除法时,有时得到有限小数,有时得到无限循环小数. 如果我们把有限小数的末尾加上无限多个0,它们就有了统一的形式. 本题的任务是:在上面的约定下,求整数除法小数点后的第n位开始 ...

  2. 51 nod 1350 斐波那契表示

    每一个正整数都可以表示为若干个斐波那契数的和,一个整数可能存在多种不同的表示方法,例如:14 = 13 + 1 = 8 + 5 + 1,其中13 + 1是最短的表示(只用了2个斐波那契数).定义F(n ...

  3. B - N皇后问题

    原文链接 一天课下,张老板研究起了国际象棋,渴望完美的他更改了棋盘的大小,在N*N的方格棋盘放置了N个皇后,希望它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的 ...

  4. JavaScript 错误监控Fundebug

    https://www.fundebug.com/ 等待接收错误 请先将Fundebug插件集成到您的应用中 测试插件 为验证集成是否成功,请在浏览器的控制台执行以下命令: fundebug.noti ...

  5. html->head->body

    ps:大师兄的博客链接http://www.imdsx.cn/ http://ui.imdsx.cn/html/ html 相当于一个人 css 相当于为这个人穿上漂亮的衣服,化妆 js    相当于 ...

  6. c++多线程基础3(mutex)

    整理自:zh.cppreference.com/w/cpp/thread 互斥锁 互斥算法避免多个线程同时访问共享资源.这会避免数据竞争,并提供线程间的同步支持.定义于头文件 <mutex> ...

  7. 【bzoj4720】[Noip2016]换教室 期望dp+最短路

    Description 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程.在可以选择的课程中,有2n节 课程安排在n个时间段上.在第i(1≤i≤n)个时间段上,两节内容相同的 ...

  8. 解决 Github用户名 变为 invalid-email-address 问题

    解决 Github用户名 变为 invalid-email-address 问题 If the identity used for this commit is wrong, you can fix ...

  9. javascript中类数组转成真正的数组

    function list() { return Array.prototype.slice.call(arguments); } var list1 = list(1, 2, 3); // [1, ...

  10. mysql5.6 的st_distance 实现按照距离远近排序。

    当前所处在的位置(113.858202 , 22.583819 ),需要查询我附近1000米内的小区,并安装由近到远的顺序排列  SELECT s.id,s.name,s.lng,s.lat, rou ...