BNU7538——Clickomania——————【区间dp】
Clickomania
64-bit integer IO format: %I64d Java class name: Main
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
Output
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】的更多相关文章
- 【BZOJ-4380】Myjnie 区间DP
4380: [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 162 Solved: ...
- 【POJ-1390】Blocks 区间DP
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5252 Accepted: 2165 Descriptio ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- BZOJ1055: [HAOI2008]玩具取名[区间DP]
1055: [HAOI2008]玩具取名 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1588 Solved: 925[Submit][Statu ...
- poj2955 Brackets (区间dp)
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...
- HDU5900 QSC and Master(区间DP + 最小费用最大流)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...
- 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 ...
- 区间dp总结篇
前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...
- Uva 10891 经典博弈区间DP
经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...
- 2016 年沈阳网络赛---QSC and Master(区间DP)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5900 Problem Description Every school has some legend ...
随机推荐
- SQL/T-SQL实例参考-2
对多关联查询,查询多中的记录,但是返回一的结果集 子查询语法 --一对多关联查询,查询多中的记录,但是返回一的结果集 SELECT C.* FROM ( SELECT A.BasicID FROM [ ...
- 13本热门书籍免费送!(Python、SpingBoot、Entity Framework、Ionic、MySQL、深度学习、小程序开发等)
七月第一周,网易云社区联合清华大学出版社为大家送出13本数据分析以及移动开发的书籍(Python.SpingBoot.Entity Framework.Ionic.MySQL.深度学习.小程序开发等) ...
- WebJars——web端静态资源的jar包
1.WebJars介绍 Web前端使用了越来越多的JS或CSS,如jQuery,Backbone.js和Bootstrap.一般情况下,我们是将这些Web资源拷贝到Java Web项目的webapp相 ...
- day3学python 字典+列表集合+文件读取
字典+列表集合+文件读取 字典示例 ************************ 各地食品的三级菜单************************* 1.使用字典嵌套字典 2.采用死循环思路 3 ...
- Cocos Creator 入门
Cocos Createor 资源 略 场景 节点树 节点与组件 坐标系 脚本 组件声明,生命周期回调 var Component = cc.Class({ // 用于序列化,可省略 name: 's ...
- 第八届山东ACM省赛F题-quadratic equation
这个题困扰了我长达1年多,终于在今天下午用两个小时理清楚啦 要注意的有以下几点: 1.a=b=c=0时 因为x有无穷种答案,所以不对 2.注意精度问题 3.b^2-4ac<0时也算对 Probl ...
- networkX如何读取存储图的二进制.dat文件
一般情况下,.dat文件存储的是图的二进制邻接矩阵. import networkx as nx G = nx.readadjlist('auth_graph.dat')
- Python3之时间模块time & datetime & calendar
一. 简介 python 提供很多方式处理日期与时间,转换日期格式是一个常见的功能. 时间元组:很多python函数用一个元组装起来的9组数字处理时间. python中时间日期格式化符号: %y 两位 ...
- PHP脚本占用内存太多,解决方案
Fatal Error: Allowed memory size of xxxxxx bytes exhausted 今天遇到服务器很多自动任务的php脚本占用服务内存过多,并且程序不在运行了. 解决 ...
- 洛谷 P4093 [HEOI2016/TJOI2016]序列(Cdq+dp)
题面 luogu 题解 \(Cdq分治+dp\) \(mx[i],mn[i]\)分别表示第\(i\)位最大,最小能取到多少 那么有 \(j < i\) \(mx[j] \le a[i]\) \( ...