Topic links: http://oj.ecustacm.cn/problem.php?id=1288
Planet X archaeologists found a number of ancient stayed password. These passwords are strung by A, B, C, D four plant seed sequence.
Careful analysis found that these password strings before and after the original should be symmetrical (that is, we say mirrored string).
Because of the age, many seeds fall off, and thus may lose features mirror.
Your mission: to set a password string now see, calculate from the original state, it should at least how many seeds fall off, it could become what it is today.
Careful analysis found that these password strings before and after the original should be symmetrical (that is, we say mirrored string).
Because of the age, many seeds fall off, and thus may lose features mirror.
Your mission: to set a password string now see, calculate from the original state, it should at least how many seeds fall off, it could become what it is today.
Entry
A plurality of sets of test data input, an input line for each test, represented now see the password string (length of not more than 1000)
Export
For each set of test data required output a positive integer, representing at least the number of seeds fall.
Sample input Copy
ABCBA
ABDCDCBABC
Sample output Copy
0
3
Ideas:
Because it is symmetrical string, which is the period of the previous paragraph and the back is the same (symmetric) then it is easy to think of this string to flip it, and then we went string after string and flip flip them before the LCS between it
Clear about the LCS is not required to be contiguous, there may be an interval between them
#include <iostream> #include <algorithm> #include <string> #include <string.h> #include <vector> #include <map> #include <stack> #include <set> #include <queue> #include <math.h> #include <cstdio> #include <iomanip> #include <time.h> #include <list> #define LL long long #define INF 0x3f3f3f3f #define ls nod<<1 #define rs (nod<<1)+1 const int maxn = 1e5 + 10 ; const LL mod = 20010905; int dp[1010][1010]; int main() { std::string a,b; while (std::cin >> a) { memset(dp, 0, sizeof(dp)); b = a; int len = a.length(); std::reverse(b.begin(), b.end()); for (int i = 1; i <= len; i++) { for (int j = 1; j <= len; j++) { if (a[i - 1] == b[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else { dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]); } } } std::cout << len - dp[len][len] << std::endl; } return 0; }