Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // I can't tell you what it really is,
- // I can only tell you what it feels like.
- #include "bits/stdc++.h"
- using namespace std;
- //#define int long long
- #define F first
- #define S second
- #define rep(i,a,n) for (int i = a; i <= n; ++i)
- #define fo(i, n) for (int i = 1; i <= n; ++i)
- #define all(v) v.begin(), v.end()
- #define pb push_back
- #define P pair < int, int >
- #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
- const int N = 3e5 + 5;
- struct node {
- node *a[26];
- int len;
- int ed;
- node () {
- rep(i,0,25)
- a[i] = NULL;
- len = 0;
- ed = 0;
- }
- };
- string s;
- node *head = new node;
- void add(node *temp, int sz) {
- int i = 0;
- while(i != sz) {
- if(temp -> a[s[i] - 'a'] == NULL)
- temp -> a[s[i] - 'a'] = new node;
- temp = temp -> a[s[i] - 'a'];
- // cout << s[i];
- i += 1;
- }
- //cout << '\n';
- temp -> len += 1;
- }
- void pre(node *temp, node *par, string &str) {
- if(temp == NULL)
- return;
- for(int i = 0; i < 26; ++i) {
- if(temp -> a[i] != NULL) {
- // str += i + 'a';
- pre(temp -> a[i], temp, str);
- // cout << str << ' ';
- temp -> ed = temp -> a[i] -> len + temp -> a[i] -> ed;
- // cout << temp -> ed << ' ' << temp -> a[i] -> len << '\n';
- // str.pop_back();
- }
- }
- }
- /*5
- a
- aaaa
- aa
- aaa
- aaa
- */
- int ans = 0;
- void dp(node *temp, string &s, int idx) {
- if(temp == NULL || temp -> a[s[idx] - 'a'] == NULL) {
- return;
- }
- if(idx + 1 == s.size()) {
- // if(s == "aaa")
- // cout << temp -> ed << ' ' << temp -> a[idx] -> len << '\n';
- ans = temp -> ed - temp -> a[s[idx] - 'a'] -> len;
- return;
- }
- //cout << idx << ' ';
- //if(temp -> a[s[idx] - 'a'] != NULL)
- int let = s[idx] - 'a';
- // cout << let << ' ';
- dp(temp -> a[let], s, idx + 1);
- }
- signed main(){
- fast;
- int t;
- cin >> t;string str;
- std::vector<string> v;
- while(t--) {
- cin >> s;
- v.pb(s);
- add(head, (int)s.size());
- }
- pre(head, head, str);
- for(auto i : v) {
- ans = 0;
- // cout << i << '\n';
- dp(head, i, 0);
- cout << ans << '\n';
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment