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;
}

  

 

最新文章

  1. Linux常用命令速查备忘
  2. Linux下通过shell脚本创建账户
  3. H-UI的前端处理验证,判断是否已经存在,比较健全的模板,可以自己添加一些校验
  4. 格而知之4:寻找EXC_BAD_ACCESS
  5. ZOJ Monthly, June 2014 月赛BCDEFGH题题解
  6. java 类 及其 执行过程
  7. Codeforces Round #243 (Div. 2) Problem B - Sereja and Mirroring 解读
  8. 笨鸟先飞之ASP.NET MVC系列之过滤器(01过滤器简介)
  9. UPDATE/INSERT用法研究
  10. Java 内部类的意义及应用
  11. C# 启动外部进程
  12. Dubbo 源码分析系列之三 —— 架构原理
  13. CentOS自带定时任务crontab
  14. Log4j 随笔
  15. Vue 使用 vuelidate 实现表单验证
  16. -webkit-line-clamp下多行文字溢出点点点...显示实例页面
  17. 五年屌丝运维工作shell精华
  18. Flowable BPMN 简单使用
  19. Android SDK 墙内更新方法
  20. android开发笔记,杂

热门文章

  1. C# 微信openid 用户信息
  2. Android日期时间选择器DatePicker、TimePicker日期时间改变事件响应(Android学习笔记)
  3. javascript webstorm用法
  4. JMeter的使用——ApacheJMeterTemporaryRootCA.crt的用法
  5. MVN package install error javac: invalid target release: 1.8
  6. MVC进阶篇(三)——model层数据验证
  7. docker kubernetes swarm spring cloud结合学习资源
  8. Unity---动画系统学习(2)---模型3种导入方式、人形动画介绍、切割动画
  9. Jenkins项目部署使用教程-----03节点添加
  10. 如果将markdown视作一门编程语言可以做哪些有趣的事情?