首页 > 代码库 > poj 1159 Palindrome - 动态规划
poj 1159 Palindrome - 动态规划
<style></style>
A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order to obtain a palindrome.
As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome.
As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome.
Input
Your program is to read from standard input. The first line contains one integer: the length of the input string N, 3 <= N <= 5000. The second line contains one string with length N. The string is formed from uppercase letters from ‘A‘ to ‘Z‘, lowercase letters from ‘a‘ to ‘z‘ and digits from ‘0‘ to ‘9‘. Uppercase and lowercase letters are to be considered distinct.
Output
Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.
Sample Input
5Ab3bd
Sample Output
2
f[j][i]表示从i这里开始,一直往后j个字符的这一段改成回文串最少需要添加的字符。然后第一维可以拉去滚动。转移是显然的。。
Code
1 /** 2 * poj 3 * Problem#1159 4 * Accepted 5 * Time:782ms 6 * Memory:748k 7 */ 8 #include<iostream> 9 #include<sstream>10 #include<cstdio>11 #include<cmath>12 #include<cstdlib>13 #include<cstring>14 #include<cctype>15 #include<queue>16 #include<set>17 #include<map>18 #include<stack>19 #include<vector>20 #include<algorithm>21 #ifdef WIN3222 #define AUTO "%I64d"23 #else24 #define AUTO "%lld"25 #endif26 using namespace std;27 typedef bool boolean;28 #define smin(a, b) (a) = min((a), (b))29 #define smax(a, b) (a) = max((a), (b))30 template<typename T>31 inline void readInteger(T& u){32 char x;33 int aFlag = 1;34 while(!isdigit((x = getchar())) && x != ‘-‘);35 if(x == ‘-‘){36 aFlag = -1;37 x = getchar();38 }39 for(u = x - ‘0‘; isdigit((x = getchar())); u = u * 10 + x - ‘0‘);40 ungetc(x, stdin);41 u *= aFlag;42 }43 44 template<typename T>class Matrix{45 public:46 T *p;47 int lines;48 int rows;49 Matrix():p(NULL){ }50 Matrix(int rows, int lines):lines(lines), rows(rows){51 p = new T[(lines * rows)];52 }53 T* operator [](int pos){54 return (p + pos * lines);55 }56 };57 #define matset(m, i, s) memset((m).p, (i), (s) * (m).lines * (m).rows)58 59 int n;60 char* str;61 62 inline void init() {63 readInteger(n);64 str = new char[(const int)(n + 3)];65 getchar();66 gets(str + 1);67 }68 #define idx(i) ((i < 0) ? (i + 3) : (i))69 70 //Matrix<int> f;71 int f[3][5005];72 inline void solve() {73 // f = Matrix<int>(3, n + 1);74 for(int i = 1; i <= n; i++)75 f[0][i] = 0;76 for(int i = 1; i < n; i++)77 f[1][i] = (str[i] == str[i + 1]) ? (0) : (1);78 int t = 2;79 for(int k = 2; k < n; k++) {80 for(int i = 1; i + k <= n; i++) {81 int j = i + k;82 if(str[i] == str[j]) f[t][i] = f[idx(t - 2)][i + 1];83 else f[t][i] = min(f[idx(t - 1)][i], f[idx(t - 1)][i + 1]) + 1;84 }85 t = (t == 2) ? (0) : (t + 1);86 }87 printf("%d", f[idx(t - 1)][1]);88 }89 90 int main() {91 init();92 solve();93 return 0;94 }
poj 1159 Palindrome - 动态规划
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。