uva 11584 Partitioning by Palindromes 线性dp
// uva 11584 Partitioning by Palindromes 线性dp
//
// 题目意思是将一个字符串划分成尽量少的回文串
//
// f[i]表示前i个字符能化成最少的回文串的数目
//
// f[i] = min(f[i],f[j-1] + 1(j到i是回文串))
//
// 这道题还是挺简单的,继续练 #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define ceil(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define gcd __gcd
#define highBit(x) (1ULL<<(63-__builtin_clzll(x)))
#define popCount __builtin_popcountll
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const long double PI = acos(-1.L); template<class T> inline T lcm(const T& a, const T& b) { return a/gcd(a, b)*b; }
template<class T> inline T lowBit(const T& x) { return x&-x; }
template<class T> inline T maximize(T& a, const T& b) { return a=a<b?b:a; }
template<class T> inline T minimize(T& a, const T& b) { return a=a<b?a:b; } const int maxn = 1008;
char s[maxn];
int f[maxn];
bool vis[maxn][maxn];
int n; bool ok(int x,int y){
while(x<y){
if (s[x]==s[y]){
x++;
y--;
}else {
return false;
}
}
return true;
} void init(){
memset(vis,0,sizeof(vis));
scanf("%s",s+1);
n = strlen(s+1);
f[0]=0;
for (int i=1;i<=n;i++)
f[i] = i; for (int i=1;i<=n;i++)
for (int j=i;j<=n;j++){
if (ok(i,j)){
vis[i][j]=1;
}
} for (int i=1;i<=n;i++)
for (int j=1;j<=i;j++){
if (vis[j][i]){
f[i] = min(f[i],f[j-1]+1);
}
}
printf("%d\n",f[n]);
} int main() {
int t;
//freopen("G:\\Code\\1.txt","r",stdin);
scanf("%d",&t);
while(t--){
init();
}
return 0;
}
uva 11584 Partitioning by Palindromes 线性dp的更多相关文章
- UVA - 11584 Partitioning by Palindromes[序列DP]
UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...
- UVa 11584 Partitioning by Palindromes【DP】
题意:给出一个字符串,问最少能够划分成多少个回文串 dp[i]表示以第i个字母结束最少能够划分成的回文串的个数 dp[i]=min(dp[i],dp[j]+1)(如果从第j个字母到第i个字母是回文串) ...
- UVa 11584 Partitioning by Palindromes (简单DP)
题意:给定一个字符串,求出它最少可分成几个回文串. 析:dp[i] 表示前 i 个字符最少可分成几个回文串,dp[i] = min{ 1 + dp[j-1] | j-i是回文}. 代码如下: #pra ...
- UVA 11584 "Partitioning by Palindromes"(DP+Manacher)
传送门 •题意 •思路一 定义 dp[i] 表示 0~i 的最少划分数: 首先,用马拉车算法求解出回文半径数组: 对于第 i 个字符 si,遍历 j (0 ≤ j < i),判断以 j 为回文中 ...
- 区间DP UVA 11584 Partitioning by Palindromes
题目传送门 /* 题意:给一个字符串,划分成尽量少的回文串 区间DP:状态转移方程:dp[i] = min (dp[i], dp[j-1] + 1); dp[i] 表示前i个字符划分的最少回文串, 如 ...
- UVa 11584 - Partitioning by Palindromes(线性DP + 预处理)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 11584 - Partitioning by Palindromes DP
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- 【线性结构上的动态规划】UVa 11584 - Partitioning by Palindromes
回文串问题.给出一个字符串,问最少可以划分为多少个字符串子串. 对于判断是否为回文串,对于不是很长的字符串,可以采取直接暴力,即从两边向中间收缩判断字符相等. bool is_pali(int l, ...
- UVA 11584 Partitioning by Palindromes (字符串区间dp)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
随机推荐
- 使用Python获取计算机名,ip地址,mac地址等等
获取计算机名 # 获取计算机名,常用的方法有三种 import os import socket # method one name = socket.gethostname() print(name ...
- Selenium 多窗口元素定位处理
以下文章来自于 上海-悠悠的博客 <Selenium2+python自动化13-多窗口.句柄(handle)> 有些页面的链接打开后,会重新打开一个窗口,对于这种情况,想在新页面上操作, ...
- 经常用到的Eclipse快捷键(更新中....)
alt+shift+s:弹出Source选项,用于生成get,set等方法. Ctrl+E:快速显示当前Editer的下拉列表 alt+shift+r:重命名 Ctrl+Shift+→/Ctrl+Sh ...
- hdu 2196(方法1:经典树形DP+方法2:树的直径)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- Java通过jedis操作redis(增删改查)
package sgh.main.powersite; import java.util.ArrayList; import java.util.HashMap; import java.util.I ...
- AC日记——病毒侵袭 hdu 2896
2896 思路: 好题: 代码: #include <queue> #include <cstdio> #include <cstring> using names ...
- flutter 快捷键
1.热重载 alt+\ 2.热重启 alt+shift+\ 3.快速生成模板 stf 直接生成有状态模板 4.模拟器中文输入法 http://www.mdpda.com/app/apk3670941. ...
- (13)python 正则表达式
匹配单个字符 f. o f和o之间是任意字符 例如:fbo123 .. 任意两个字符 \.用来匹配. 边界匹配 the 表示包含the的任何字符串 ^from 表示以from开头的所 ...
- 欧拉图和欧拉圈-Play On Words(UVa10129)
欧拉路和欧拉圈,简言之就是,从无向图的一个结点出发,走一条路/圈,每条边恰好经过一次,即一笔画问题 欧拉定理:一个无向图最多只有两个奇结点,那么我们就从一个奇结点出发,到另一个结点为之,一定有一条欧拉 ...
- Eclipse Build all and build project not working - jar missing
Eclipse Build all and build project not working - jar missing