Submission #1161965


Source Code Expand

#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;
        if (p->a[lc]->leaf == false && p->ctr == p->a[lc]->ctr) {
          p = p->a[lc];
          s++;
        }
        else break;
      }
      if (s > 0) {
        a[i] = p;
        a[i]->skip += s;
      }
      a[i]->shorten();
    }
  }

  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 (GCC 5.4.1)
Score 0
Code Size 2127 Byte
Status TLE
Exec Time 6307 ms
Memory 101660 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 134 ms 29696 KB
02.txt AC 134 ms 29696 KB
03.txt AC 134 ms 29696 KB
04.txt AC 134 ms 29696 KB
05.txt AC 210 ms 72576 KB
06.txt AC 664 ms 93952 KB
07.txt AC 2137 ms 95232 KB
08.txt TLE 6307 ms 95360 KB
09.txt TLE 6307 ms 95488 KB
10.txt AC 123 ms 96976 KB
11.txt AC 128 ms 10112 KB
12.txt AC 118 ms 13952 KB
13.txt AC 117 ms 18304 KB
14.txt AC 602 ms 89600 KB
15.txt TLE 6307 ms 97536 KB
16.txt TLE 6307 ms 98768 KB
17.txt AC 126 ms 101660 KB
18.txt AC 2092 ms 2048 KB
19.txt AC 2099 ms 2048 KB
20.txt AC 2530 ms 2304 KB
21.txt AC 2531 ms 2304 KB
22.txt AC 2549 ms 2304 KB
23.txt AC 130 ms 18816 KB
24.txt AC 129 ms 10240 KB
25.txt AC 119 ms 13952 KB
26.txt AC 1595 ms 2048 KB
27.txt AC 415 ms 11264 KB
28.txt AC 335 ms 14976 KB
29.txt AC 2995 ms 14464 KB
30.txt AC 180 ms 23040 KB
31.txt AC 1164 ms 30464 KB
32.txt TLE 6303 ms 23040 KB
33.txt TLE 6304 ms 37176 KB
34.txt TLE 6304 ms 32132 KB
35.txt TLE 6304 ms 41344 KB
36.txt AC 140 ms 34048 KB
37.txt AC 101 ms 51604 KB
38.txt AC 100 ms 51604 KB
39.txt AC 2 ms 1024 KB
40.txt AC 2 ms 1024 KB
s1.txt AC 2 ms 1024 KB
s2.txt AC 2 ms 1024 KB