Problem DescriptionSquare String:
String target of type string s = t + t:
For the string s, remove a number of characters to make it a square string, and find the maximum length of the square string.
Idea and Realization
The maximum subsequence length (longest common subsequence) of two substrings is divided into two halves, and the longest square string length is 2*ans.
const int MAXN = m;
int C[MAXN][MAXN] = {0};
S-Input string
//left string is s[0 ... ( s2-1)]
//Right string is s[s2 ...]
int LCS (string s, int s2) {
if (S2 >= s.size ()) return 0;
int size1 = s2, size2 = s.size ()-S2; for (int i = 1; I <= size1. i++) {for
(int j = 1; J <= Size2; j +) {
if (s[i-1] = = S[s2 + j-1]) {
C I [j] = c[i-1][j-1] + 1;
}
else{
C[i][j] = max (c[i-1][j], c[i][j-1);
}}
return c[size1][size2];
}
To reduce the number of LCS, I filter the characters that appear only once in the input string, but it doesn't seem to be necessary.
int Main () {string S;
Cin >> S;
int map[26] = {0};
for (int i = 0; i < s.size (); ++i) {map[s[i]-' a ']++;
} string ns;
for (int i = 0; i < s.size (); ++i) {if (Map[s[i]-' a '] > 1) {ns.push_back (s[i]);
} int ans = 0;
for (int i = 1; i < ns.size (); i++) {int x = LCS (ns, i);
if (x > ans) ans = x;
cout << ans * 2 << Endl;
System ("pause");
return 0; }