New Ket Net-constructing palindrome string

Source: Internet
Author: User

Today's topic is very interesting, in the topic given a string to construct a palindrome substring, the topic is as follows:



At first glance, this topic seems to be different, constructing palindrome strings in a given string. Because the length of the character is between [1,1000], the minimum length of the palindrome is 1. The biggest characteristic of palindrome string is symmetry. Then there are two forms of the final text string:

(1) xxa1xa2xxa3xa3xxa2xa1x;

(2) xa1xa2xa3xa4xa3xa2xa1x;

where x represents other characters that do not constitute a palindrome string (X can represent 0 or more occurrences of other non-return characters), then we get the palindrome string is A1A2A3A3A2A1 or A1A2A3A4A3A2A1;

For the first case we reverse the string and then compare it to the original string:

Xxa1xa2xxa3xa3xxxa2xa1x (positive order, original string)

XA1XA2XXXA3XA3XXA2XA1XX (reverse)

Then we can see that the palindrome substring is the common substring of the original string and its inverse string, and the original problem is transformed into a familiar problem. Then we will wonder if the characters appearing in the common substring must be the longest string of strings to be asked. The answer is yes, because in both the original string and the inverse string, before the A1 appears, there must be no identical letters (that is, XX and x do not have common characters), because the inverse string begins with the same x as the end of the original string, if the red and green parts have common characters, then the blue and green parts also have common characters, The palindrome string in the original string will be extended to the front and back two A1 direction, this and the original palindrome string contradiction, so the red and green parts must not have the same character, then this logic also applies to other x represented by the character, then the conclusion is:

The longest common substring formed by the original string and its inverse string is the longest string of strings of the original string, which means that our problem is converted to a common substring of two strings ....

The following code runs a timeout:

Class solution{public:void Search_route (String &a, string &b, int xlabel, int ylabel, int **p) {if (Xlabel = = 0 | |
		Ylabel = = 0) return;
		BOOL flag = FALSE;
			if (a[xlabel-1] = = B[ylabel-1]) {Stk.push (a[xlabel-1]);
				if (stk.size () = = P[a.size ()][b.size ()]) {result = STK;
				Stk.pop ();
			Return
		} flag = true; } if (P[xlabel][ylabel] = = P[xlabel-1][ylabel] | | p[xlabel][ylabel] = = P[xlabel][ylabel-1]) {//If you can go up or go left if (p [Xlabel]
			[Ylabel] = = P[xlabel-1][ylabel]) Search_route (A, B, Xlabel-1, Ylabel, p);
		if (p[xlabel][ylabel] = = P[xlabel][ylabel-1]) Search_route (A, B, Xlabel, YLABEL-1, p);  } else Search_route (A, B, Xlabel-1, YLABEL-1, p);
	can only go diagonally if (flag) Stk.pop ();
		} stack<char> Lcslen (string A, string b) {int sa = a.size (), SB = B.size ();
		if (sa = = 0 | | sb = = 0) return result;
		int **p = new int *[sa + 1];
		for (int i = 0; i < SA + 1; i++) p[i] = new INT[SB + 1]; for (int i = 0; i < SA + 1; i++) P[i][0] = 0;
		for (int i = 0; i < SB + 1; i++) p[0][i] = 0;  for (int i = 0, i < sa; i++) {for (int j = 0; J < SB; J + +) {if (a[i] = = B[j]) {p[i + 1][j + 1] = P[i][j]
				+ 1;
				} else{P[i + 1][j + 1] = max (p[i][j + 1], P[i + 1][j]);
		}}} int lena = sa, LenB = sb;
		Search_route (A, B, Lena, LenB, p);
		for (int i = 0; i < sa; i++) Delete p[i];
		Delete p;
	return result;
	} stack<char>stk;
stack<char>result;

};
	int main () {string origin, inverse;
	stack<char>re;
	Getline (CIN, origin);
	Inverse = origin;
	Reverse (Inverse.begin (), Inverse.end ());
	Solution AA; while (Origin[0]) {re = aa.
		Lcslen (Origin,inverse);
		cout << origin.size ()-re.size () << Endl;
		Getline (CIN, origin);
		Inverse = origin;
	Reverse (Inverse.begin (), Inverse.end ());
} return 0; }
The code runs out of time, and the result of the operation is not obtained when the string exceeds a certain length ....


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.