Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- const int ALPHABET_SIZE = 2;
- struct node {
- node *children_of_this_node[ALPHABET_SIZE];
- bool end_of_word;
- node() {
- for(int i = 0; i < ALPHABET_SIZE; i++) {
- children_of_this_node[i] = NULL;
- }
- end_of_word = false;
- }
- };
- void insert_word(node * trie, string & s) {
- node * tmp = trie;
- for(int i = 0; i < (int) s.size(); i++) {
- int current_char = s[i] - 'a';
- if(tmp -> children_of_this_node[current_char] == NULL) {
- tmp -> children_of_this_node[current_char] = new node();
- }
- tmp = tmp -> children_of_this_node[current_char];
- }
- tmp -> end_of_word = true;
- }
- bool search_word(node * trie, string & s) {
- node * tmp = trie;
- for(int i = 0; i < (int) s.size(); i++) {
- int current_char = s[i] - 'a';
- if(tmp -> children_of_this_node[current_char] == NULL) {
- return false;
- }
- tmp = tmp -> children_of_this_node[current_char];
- }
- return tmp -> end_of_word;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- int n;
- cin >> n;
- vector<string> v(n);
- for(int i = 0; i < n; i++) {
- cin >> v[i];
- }
- for(int i = 0; i < n; i++) {
- for(int j = i + 1; j < n; j++) {
- if((int) v[i].size() < (int) v[j].size()) {
- swap(v[i], v[j]);
- }
- }
- }
- node * tries[n - 1];
- for(int i = 0; i < n - 1; i++){
- tries[i] = new node();
- }
- for(int at = 1; at < n; at++) {
- for(int i = 0; i < (int) v[at].size(); i++) {
- string tmp = "";
- for(int j = i; j < min((int) i + 60, (int) v[at].size()); j++) {
- tmp += v[at][j];
- insert_word(tries[at - 1], tmp);
- }
- }
- }
- int result = 0;
- for(int i = 0; i < (int) v[0].size(); i++) {
- string tmp = "";
- for(int j = i; j < min(i + 60, (int) v[0].size()); j++) {
- tmp += v[0][j];
- bool ok = true;
- for(int k = 0; k < n - 1; k++) {
- if(!search_word(tries[k], tmp)) {
- ok = false;
- break;
- }
- }
- if(ok) {
- result = max(result, (int) tmp.size());
- }
- }
- }
- cout << result << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement