Poj-1159-Palindrome-learning scrolling Array

Source: Internet
Author: User

Question: Here is a string that can contain at least a few characters to make it a return string. Practice: Set a [I] to this string, and B [I] to the reverse string of this string. Then, the longest common subsequence of a [I] and B [I] is the largest input string in the string. Then, the length of the string minus the maximum length of the input string is required. Formula for finding the longest common subsequence: dp [I] [j] = max (dp [I-1] [j], dp [I] [J-1]) if (a [I] = B [I]) dp [I] [j] = max (dp [I] [j], dp [I-1] [J-1] + 1); If you directly find it, it is bound to open a 5001*5001 array, set MLE. There are two solutions: 1. Open the short int type array. This is the result returned by poj: 2. Use a dynamic array. According to the dp rolling process, we can know that the value of dp [I] [j] is not related to the value of dp [I-2] [0... n. Then the value of dp [I] [j] can be overwritten on dp [I-2] [j. That is, dp [I] [j] Is dp [I % 2] [j]. The result returned by poj is as follows: Compared with the above two methods, it is obvious that the method 2 is very space-saving, it takes a little longer. 1. short int array [html] # include <iostream> # include <stdio. h> # include <string. h> # define max (a, B) (a> B? A: B) using namespace std; short int dp [5001] [5001]; int main () {int a [5001]; int B [5001]; int I, j; int n; char str; cin> n; getchar (); for (I = 1; I <= n; I ++) {scanf ("% c ", & str); a [I] = str; B [n-I + 1] = str ;}for (I = 0; I <= n; I ++) {dp [I] [0] = 0; dp [0] [I] = 0 ;}for (I = 1; I <= n; I ++) {for (j = 1; j <= n; j ++) {dp [I] [j] = max (dp [I] [J-1], dp [I-1] [j]); if (a [I] = B [j]) {dp [I] [j] = max (dp [I] [j], dp [I-1] [J-1] + 1) ;}} int len; len = dp [n] [n]; printf ("% d \ n", n-len); return 0;} 2, rolling array [html] # include <iostream> # include <stdio. h> # include <string. h> using namespace std; int main () {int a [5001]; int B [5001]; int dp [10] [5001]; int I, j; int n; char str; cin> n; getchar (); for (I = 1; I <= n; I ++) {scanf ("% c", & str ); a [I] = str; B [n-I + 1] = str;} dp [1] [0] = dp [0] [0] = 0; for (I = 0; I <= n; I ++) {dp [0] [I] = 0;} for (I = 1; I <= n; I ++) {for (j = 1; j <= n; j ++) {dp [I % 2] [j] = max (dp [I % 2] [J-1], dp [(I-1) % 2] [j]); if (a [I] = B [j]) {dp [I % 2] [j] = max (dp [I % 2] [j], dp [(I-1) % 2] [J-1] + 1) ;}} int len; len = dp [n % 2] [n]; printf ("% d \ n", n-len); return 0 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.