fork download
#include <iostream>
#include <cstdio>
#include <map>

using namespace std;

const int MAXLEN = 1000; // максимальная длина всех строк
const int inf = 1000;

struct vertex {
    map <char, int> next;
    bool leaf;
};

int sz, len;
vertex tr[MAXLEN];

void init_trie () {
    sz = 0;
    for (int i = 0; i < MAXLEN; ++i) {
        tr[i].next.clear ();
        tr[i].leaf = false;
    }
    sz++;
}

void add_string (string s) {
    int v = 0;
    for (int i = 0; i < s.length (); ++i) {
        if (!tr[v].next.count (s[i])) {
            tr[v].next[s[i]] = sz++;
        }
        if (tr[v].next.size () > 1) {
            len = min(len, i);
        }
        v = tr[v].next[s[i]];
    }
    len = min(len, (int)s.length ());
    tr[v].leaf = true;
}

main () {
    int n;
    cin >> n;
    init_trie();
    len = inf;
    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        add_string (s);
        cout << len << "\n";
    }
}
Success #stdin #stdout 0s 3464KB
stdin
4
abcd
abc
abcd
aa
stdout
4
3
3
1