palindrome Stringtime limit: ms | Memory limit:65535 KBDifficulty:4
-
Describe:
The so-called Palindrome string, is a string, from left to right reading and right-to-left reading is exactly the same, such as "ABA". Of course, the question we give you is no longer as simple as judging whether a string is a palindrome string. Now ask you, give you a string, you can add characters anywhere, at least add a few more characters, you can make this string a palindrome string.
-
- Input
- The first line gives the integer N (0<n<100)
The next n rows, one string per line, and no more than 1000 per string length.
- Output
- Minimum number of characters to be added per line of output
- Sample input
-
1ab3bd
- Sample output
-
2
-
Analysis: The original string is reversed first, then the inverse of the string and the original string of the longest common sub-sequence .
Code:
<span style= "font-size:18px;" > #include <cstdio> #include <iostream> #include <string> #include <algorithm> #include < cstring>using namespace std; int dp[1000][1000]; string str,tmp; int n; int main () {while (scanf ("%d", &n)!=eof) {while (n--) {cin>>str; TMP = STR; Reverse (Tmp.begin (), Tmp.end ()); for (int i = 0; I<str.size (), i++) {for (int j = 0; J<tmp.size (); j + +) { if (str[i]! = Tmp[j]) {dp[i+1][j+1] = max (Dp[i][j+1],dp[i+1][j]) ; } else {dp[i+1][j+1] = dp[i][j]+1; }}} printf ("%d\n", Str.size ()-Dp[str.size ()][tmp.size ()]); }} return 0;} </span>
Nyoj 37 Text string