Written question: 2017.9.10 Archie Art School Recruit Programming Questions "square string" problem solving ideas __ Programming

Source: Internet
Author: User
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; }

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.