题意:给定一个字符串,求出它最少可分成几个回文串。

析:dp[i] 表示前 i 个字符最少可分成几个回文串,dp[i] = min{ 1 + dp[j-1] | j-i是回文}。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#define debug() puts("++++");
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 5;
const int mod = 2000;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
char s[maxn];
int dp[maxn]; bool judge(int i, int j){
while(i < j){
if(s[i] != s[j]) return false;
++i, --j;
}
return true;
} int main(){
int T; cin >> T;
while(T--){
scanf("%s", s);
n = strlen(s);
memset(dp, INF, sizeof dp);
dp[0] = 1;
for(int i = 1; i < n; ++i){
for(int j = 0; j < i; ++j)
if(judge(j, i)) dp[i] = min(dp[i], 1 + dp[j-1]);
else dp[i] = min(dp[i], dp[i-1]+1);
}
printf("%d\n", dp[n-1]); }
return 0;
}

UVa 11584 Partitioning by Palindromes (简单DP)的更多相关文章

  1. uva 11584 Partitioning by Palindromes 线性dp

    // uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...

  2. 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 ...

  3. UVa 11584 Partitioning by Palindromes【DP】

    题意:给出一个字符串,问最少能够划分成多少个回文串 dp[i]表示以第i个字母结束最少能够划分成的回文串的个数 dp[i]=min(dp[i],dp[j]+1)(如果从第j个字母到第i个字母是回文串) ...

  4. UVA 11584 "Partitioning by Palindromes"(DP+Manacher)

    传送门 •题意 •思路一 定义 dp[i] 表示 0~i 的最少划分数: 首先,用马拉车算法求解出回文半径数组: 对于第 i 个字符 si,遍历 j (0 ≤ j < i),判断以 j 为回文中 ...

  5. 区间DP UVA 11584 Partitioning by Palindromes

    题目传送门 /* 题意:给一个字符串,划分成尽量少的回文串 区间DP:状态转移方程:dp[i] = min (dp[i], dp[j-1] + 1); dp[i] 表示前i个字符划分的最少回文串, 如 ...

  6. UVA 11584 - Partitioning by Palindromes DP

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  7. UVA 11584 Partitioning by Palindromes (字符串区间dp)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  8. UVa 11584 - Partitioning by Palindromes(线性DP + 预处理)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. UVA - 11584 Partitioning by Palindromes(划分成回文串)(dp)

    题意:输入一个由小写字母组成的字符串,你的任务是把它划分成尽量少的回文串,字符串长度不超过1000. 分析: 1.dp[i]为字符0~i划分成的最小回文串的个数. 2.dp[j] = Min(dp[j ...

随机推荐

  1. [WASM] Run WebAssembly in Node.js using the node-loader

    WebAssembly is great for targeting performance bottlenecks in the browser. Now with node-loader, we ...

  2. [Unit Testing] Fundamentals of Testing in Javascript

    In this lesson, we’ll get the most fundamental understanding of what an automated test is in JavaScr ...

  3. 【JavaScript】数据类型

    学习不论什么一种程序设计语言.数据类型都是不可缺少的一部分内容,非常基础,也非常重要.该用何种数据类型定义变量.这也是编程中最基础的一项. ECMAScript中有5种简单数据类型:Undefined ...

  4. 函数柯里化 curry

    一.函数柯里化的特性: (1)参数复用 $.ajax // 示例一 function ajax(type,url,data) { var xhr = new XMLHttpRequest(); xhr ...

  5. openERP邮件(发信、收信)

    openERP里的邮件处理主要有个2个模块处理 mail -核心 fetchmail -接收邮件     Alias domain和alias name     配置domain alias. Set ...

  6. HDU1114 Piggy-Bank 【全然背包】

    Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  7. background-size使用参考指南

    语法:background-size :[ <length> | <percentage> | auto ]{1,2} | cover | contain相关属性: backg ...

  8. BusHelper

    https://github.com/eltld/-BusHelper https://github.com/eltld/BusLineSAX

  9. MeiTuanLocateCity

    https://github.com/eltld/MeiTuanLocateCity

  10. 【bzoj1433】[ZJOI2009]假期的宿舍

    按要求连边,跑匈牙利 #include<algorithm> #include<iostream> #include<cstdlib> #include<cs ...