Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <string >
- #include <algorithm>
- #include <cmath>
- using namespace std;
- #define ll long long
- string s = "";
- struct node;
- struct edge;
- struct edge {
- ll left;
- ll right;
- node * to = nullptr;
- };
- struct node {
- map < char, edge > next;
- };
- void add(node * root, ll left, ll right){
- node * now = root;
- ll n = s.size();
- for (int i = left; i < right; i++){
- ll first_id = i;
- if (now->next.find(s[i]) != now->next.end()){
- ll l = now->next[s[i]].left;
- ll r = now->next[s[i]].right;
- ll j;
- for (j = l; j < r; j++){
- if (s[i] == s[j]){
- i++;
- }
- else {
- break;
- }
- }
- if (j == r){
- now = now->next[s[first_id]].to;
- i--;
- continue;
- }
- node * q = new node;
- q->next[s[j]].to = now->next[s[first_id]].to;
- q->next[s[j]].left = j;
- q->next[s[j]].right = r;
- root->next[s[first_id]].to = q;
- root->next[s[first_id]].right = j;
- q->next[s[i]].to = new node;
- q->next[s[i]].left = i;
- q->next[s[i]].right = n;
- break;
- }
- else {
- now->next[s[i]].to = new node;
- now->next[s[i]].left = i;
- now->next[s[i]].right = right;
- break;
- }
- }
- }
- long long num = 1;
- void dfs(node * root){
- cout << num << endl;
- for (auto elem : root->next){
- cout << elem.first << " " << elem.second.left << " " << elem.second.right << endl;
- }
- cout << endl;
- for (auto elem : root->next){
- num++;
- dfs(root->next[elem.first].to);
- }
- }
- int main(){
- ll n;
- cin >> n;
- node * borh = new node;
- for (int i = 0; i < n; i++){
- string x;
- cin >> x;
- ll l = s.size();
- s += x;
- ll r = s.size();
- add(borh, l , r);
- }
- cout << s << endl;
- dfs(borh);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement