N - No Prefix Set (trie tree to find its first side is more than a string for the prefix string of the other party)

topic

Output Format
Output GOOD SET if the set is valid.
Else, output BAD SET followed by the first string for which the condition fails.

Sample Input00

7
aab
defgab
abcde
aabcde
cedaaa
bbbbbbbbbb
jabjjjad

Sample Output00

BAD SET
aabcde

Sample Input01

4
aab
aac
aacghgh
aabghgh

Sample Output01

BAD SET
aacghgh

Explanation
aab is prefix of aabcde. So set is BAD SET and it fails at string aabcde.

Explanation

Use a dictionary tree, without inquiry, since this question only to find a string of non-compliance, direct insertion function improvement.
Set a global array of deposit ans answer. Each character is inserted into the trie nodes simultaneously update flag, the recording flag is covered i-node string, which is the value of the flag i. The initial value of each flag are both -1 nodes, but in fact specifically covered by whom we do not care, only care about the node before this position is not already occupied the string, indicating the presence of strings on this route. If the current string is not the last one flag value is -1, indicating that the current string shorter than the upper link of a string (Section I), then the result is.
if (! (node-> = -1 exist) || (* P == '\ 0' && node-> In Flag = -1)!) {
strcpy (ANS, Word);
return n-;
}
This is the right if the reason half written, for example abcabc, abc after experiencing this situation, it is easy to fall out of the case. Then talk about another case, the existing string of short, long strings encountered later. For example abc, abcabc (example not more, in the compact).
We should be in the first position c of the results have been realized. Insert the last character of each character in the dictionary tree is that we will exist which updates the value of the current string of numbers i, likewise we do not care how much it values what is, in short, this node exist if the value is not the initial value -1. Description This node is already at the end of a string of Article (Article i) of the, to which both the current string of characters that stop there or there are more characters have been superimposed above a bar with string. So with the wording if the left half.
Remember to read the rest of the string, not into the dictionary tree just fine after finding the results, the final output ans.

expand

While this question is not concerned with the overlapping of several strings, but can be determined to overlap with the first few following code string.

#include <cstdio>
#include <cstring>
#include <cstdlib>
typedef struct node{
    int cont;
    struct node *next[26];
    int exist;
    int flag;
}trieNode, *Trie;
char ans[100];
Trie  buildtrieNode(){
    Trie node = (Trie)malloc(sizeof(trieNode));
    node->cont = 0;
    node->exist = -1;
    node->flag = -1;
    memset(node->next, 0, sizeof(node->next));
    return node;
}
int trieNode_insert(char *word, Trie root, int n){
    Trie node = root;
    int id;
    char *p = word;
    while(*p){
        id = *p - 'a';
        if(node->next[id] == NULL){
           node->next[id] = buildtrieNode();
        }
        node = node->next[id];

        p++;
        node->cont++;
        if((node->exist != -1)||(*p == '\0' && node->flag != -1)){
            strcpy(ans, word);
            return n;
        }
        node->flag = n;
    }
    node->exist  = n;
    return 0;
}
int main(){
    int n, i;
    char s[1000];
    int cont = 0;
    Trie root = buildtrieNode();
    int flag = 0;
    scanf("%d", &n);
    for(i = 1; i <= n; i++){
        scanf("%s", s);
        if(!flag)
        flag = trieNode_insert(s, root, i);

    }
    if(flag) printf("BAD SET\n%s", ans);
    else printf("GOOD SET");
    return 0;
}
发布了52 篇原创文章 · 获赞 2 · 访问量 901

Guess you like

Origin blog.csdn.net/qq_44714572/article/details/97406162