HDU 2328 Corporate Identity (trie longest common substring)

Source: Internet
Author: User
Tags first string

Test instructions the longest common substring with the smallest dictionary order of the output n strings

It is possible to enumerate all the substrings of the first string and then to match each string KMP more complex and wait to find and HDU2846 (the number of times that the pattern string appears in n strings) similar to the one that can be used to handle a string of substrings in trie. We insert all suffixes of the first string into the T The longest common substring in rie must be inside this trie because it is definitely the substring of the first string that can be found when you insert the suffix of the following string

1. For a node that is not in the first string, it can break directly in the first string.

2. The current prefix appears in the previous I string less than I so also can break because there must be some strings already do not include this prefix

So the insertion behind us is just a trie based on the first insertion. The longest string inserted is the answer when inserting the suffix of the last string.

#include <bits/stdc++.h> using namespace std;
const int N = 30005;
int trie[n][26], cnt[n], L;

Char S[n];
    void Inittrie () {L = 1;
    memset (CNT, 0, sizeof (CNT));
memset (trie, 0, sizeof (trie));
    } int Inserttrie (char s[], int id) {int r = 0, i = 0, len = 0, J;
        while (S[i]) {j = s[i++]-' a ';
            if (!trie[r][j]) {if (id = = 1) trie[r][j] = l++;  else break;
        No more in the first string.} r = Trie[r][j];
        if (Cnt[r] + 1 >= ID) cnt[r] = ID;  else break;   The current prefix has occurred less than the number of times the ID indicates that the prefix is not a substring of the preceding string without looking forward ++len;
The length of the current prefix} return len;
    } int main () {int n;
        while (scanf ("%d", &n), N) {Inittrie ();//Initialize trie int len = 0;

        Char ans[n] = "IDENTITY LOST", t[n];
            for (int i = 1; I <= n; ++i) {scanf ("%s", s);
                for (int j = 0; s[j]; ++j) {int k = Inserttrie (S + j, i); if (!k || I < n) continue;
                The last one by one strings need to be processed strncpy (T, S + J, K), t[k] = 0; if (k > Len | |
                    (k = = Len && strcmp (ans, T) > 0))
            strcpy (ans, t), len = k;
    }} puts (ANS);
} return 0;
 }//last modified:2015-10-05 15:50 CST
Corporate Identity

Problem Description beside other services, ACM helps companies-clearly state their "corporate identity", which includes Company logos but also and other signs, like trademarks. One of such companies is Internet Building Masters (IBM), which have recently asked ACM for a to help with their new identity. IBM do not want to change their existing logos and trademarks completely, because their customers is used to the old one S. Therefore, ACM would only change existing trademarks instead of creating new ones.

After several other proposals, it is decided to take all existing trademarks and find the longest common sequence of Lett ERS is contained in all of them. This sequence is graphically emphasized to form a new logo. Then, the old trademarks may still is used while showing the new identity.

Your task is to find such a sequence.
Input the input contains several tasks. Each task begins with a line containing a positive integer N, the number of trademarks (2≤n≤4000). The number is followed by N lines and each containing one trademark. Trademarks'll is composed only from lowercase letters, the length of each trademark would be at least 1 and at most C Haracters.

After the last trademark, the next task begins. The last task was followed by a line containing zero.
Output for each task, output a single line containing the longest string contained as a substring in all trademarks. If there is several strings of the same length, print the one and that is lexicographically smallest. If There is no such non-empty string, output the words "IDENTITY LOST" instead.
Sample Input
3 Aabbaabb abbababb Bbbbbabb 2 xyz ABC 0
Sample Output
ABB IDENTITY LOST

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.