Submission #1161983


Source Code Expand

#pragma optimization_level 3
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N, Q;
string S[100000];
int p[256];

class Trie {
public:
  Trie *a[26];
  int ctr;
  bool leaf;
  int vc, lc;
  int skip;

  Trie() {
    for (int i=0; i<26; i++) a[i] = NULL;
    leaf = false;
    ctr = 0;
    vc = 0;
    lc = 0;
    skip = 0;
  }
  void insert(const char *s) {
    if (*s == '\0') {
      leaf = true;
      ctr++;
      return;
    }
    int k = *s - 'a';
    if (a[k] == NULL) {
      a[k] = new Trie();
      vc++;
      lc = k;
    }
    a[k]->insert(s+1);
    ctr++;
  }

  void shorten() {
    for (int i=0; i<26; i++) {
      if (a[i] == NULL) continue;
      Trie *p = a[i];
      if (p->leaf || p->vc != 1) continue;
      int s = 0;
      while (true) {
        int lc = p->lc;
        Trie *np = p->a[lc];
        if (np->vc == 1 && np->leaf == false) {
          p = p->a[lc];
          s++;
        }
        else break;
      }
      if (s > 0) {
        a[i] = p;
        a[i]->skip += s;
      }
      a[i]->shorten();
    }
  }

  inline int find(const char *s) {
    s += skip;
    if (*s == '\0') return leaf;
    int c = leaf;
    int k = *s - 'a';
    for (int i=0; i<26; i++) {
      if (a[i] != NULL && p[i] < p[k]) c += a[i]->ctr;
    }
    if (a[k] == NULL) return c;
    c += a[k]->find(s+1);
    return c;
  }
};


signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  cin >> N;
  rep(i, N) cin >> S[i];
  cin >> Q;

  Trie trie;
  rep(i, N) trie.insert(S[i].c_str());
  trie.shorten();
  rep(i, Q) {
    int k; cin >> k;
    k--;
    rep(i, 26) {
      char c;
      cin >> c;
      p[c - 'a'] = i;
    }
    cout << trie.find(S[k].c_str()) << "\n";
  }
  return 0;
}

Submission Info

Submission Time
Task E - Lexicographical disorder
User funcsr
Language C++14 (Clang 3.8.0)
Score 0
Code Size 2164 Byte
Status TLE
Exec Time 6307 ms
Memory 115704 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 1100
Status
AC × 2
AC × 34
TLE × 8
Set Name Test Cases
Sample s1.txt, s2.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, s1.txt, s2.txt
Case Name Status Exec Time Memory
01.txt AC 502 ms 26624 KB
02.txt AC 494 ms 26624 KB
03.txt AC 493 ms 26624 KB
04.txt AC 493 ms 26624 KB
05.txt AC 558 ms 72192 KB
06.txt AC 1014 ms 95872 KB
07.txt AC 2514 ms 97152 KB
08.txt TLE 6307 ms 97152 KB
09.txt TLE 6307 ms 99328 KB
10.txt AC 437 ms 101632 KB
11.txt AC 471 ms 10240 KB
12.txt AC 465 ms 13440 KB
13.txt AC 474 ms 17024 KB
14.txt AC 924 ms 91392 KB
15.txt TLE 6307 ms 99072 KB
16.txt TLE 6307 ms 103424 KB
17.txt AC 444 ms 115704 KB
18.txt AC 2424 ms 3840 KB
19.txt AC 2434 ms 3840 KB
20.txt AC 2889 ms 3968 KB
21.txt AC 2884 ms 3968 KB
22.txt AC 2908 ms 3968 KB
23.txt AC 476 ms 18560 KB
24.txt AC 470 ms 10368 KB
25.txt AC 470 ms 13568 KB
26.txt AC 1975 ms 3840 KB
27.txt AC 757 ms 12928 KB
28.txt AC 653 ms 16512 KB
29.txt AC 3133 ms 16256 KB
30.txt AC 534 ms 24064 KB
31.txt AC 1510 ms 32128 KB
32.txt TLE 6304 ms 22784 KB
33.txt TLE 6305 ms 38272 KB
34.txt TLE 6305 ms 32640 KB
35.txt TLE 6305 ms 41344 KB
36.txt AC 513 ms 30848 KB
37.txt AC 422 ms 59388 KB
38.txt AC 424 ms 59388 KB
39.txt AC 2 ms 2560 KB
40.txt AC 2 ms 2560 KB
s1.txt AC 2 ms 2560 KB
s2.txt AC 2 ms 2560 KB