摘要:
String to Palindrome题目大意:给出一个字符串s,现在可以进行3种操作(添加字母,删除字母,替换字母),将其变成回文串,求出最少的操作次数。比如abccda,可以用删除操作,删除b,d两步可变成回文;但如果用替换操作,把b换成d则只需要1步。分析:刚开始我一直考虑它是否具有最优子结构性质,直到现在,还是不明白为什么可以用动态规划来做,大神若是看见,还望指教。 由于添加字母和删除字母的效果是一样的,因此我们这里就只进行删除和替换操作。令dp[i][j]表示从第 i 到第 j 个字母变成回文所需要最少的操作数。 转移方程为:当是s[i]==s[j]时,dp[i][j] = d.. 阅读全文
摘要:
题目大意:输入一个由小写字母组成的字符串,你的任务是把它划分成尽量少的回文串。比如racecar本身就是回文串;fastcar只能分成7个单字母的回文串;aaadbccb最少可分成3个回文串:aaa、d、bccb。字符串的长度不超过1000。分析:令dp[i]表示从第1个到第 i 个字符所组成的最少回文串数。 我们考虑如果前k个字符构成了1个回文,那么前k+1个字符最多构成2个回文,如果这些字符都相同,那么也只是1个回文串。所以如果第 j 个字母到第 i 个字母能构成回文,那么dp[i] = min(dp[i],dp[j-1]+1);初始条件是:dp[i]=i;代码如下: 1 # inclu. 阅读全文
摘要:
连连看Time Limit: 20000/10000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13730Accepted Submission(s): 3579Problem Description“连连看”相信很多人都玩过。没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子。如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子),而且线的转折次数不超过两次,那么这两个棋子就可以在棋盘上消去。不好意思,由于我以前没有玩过连连看,咨询了同学的意 阅读全文