Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- #include <algorithm>
- #include <set>
- #include <queue>
- #include <map>
- #include <unordered_map>
- #include <unordered_set>
- #include <cstdio>
- #include <cassert>
- #include <climits>
- #include <vector>
- #include <cstring>
- using namespace std;
- namespace IO {
- /** Defines */
- #define ENDLINE writeWord("\n")
- #define SPACE writeWord(" ")
- /** Interface */
- template <class T = int>
- inline T readInt(); // skip spaces, read signed int
- inline int readUInt(); // skip spaces, read unsigned int
- inline int readChar(); // skip spaces, read char
- inline void readWord( char *s ); // skip spaces, read word
- template <class T>
- inline void writeInt( T x );
- inline void writeChar( int x ); // you may use putchar() instead of it
- inline void writeWord( const char *s );
- inline void flush();
- /** Read */
- static const int buf_size = 4096;
- inline char getchar_fast() { // you may use getchar() instead of it
- static char buf[buf_size];
- static int len = 0, pos = 0;
- if (pos == len)
- pos = 0, len = fread(buf, 1, buf_size, stdin);
- if (pos == len)
- return -1;
- return buf[pos++];
- }
- inline int readChar() {
- int c = getchar_fast();
- while ((c < 32 || c > 126) && c != -1)
- c = getchar_fast();
- return c;
- }
- inline int readUInt() {
- int c = readChar(), x = 0;
- while ('0' <= c && c <= '9')
- x = x * 10 + c - '0', c = getchar_fast();
- return x;
- }
- template <class T>
- inline T readInt() {
- int s = 1, c = readChar();
- T x = 0;
- if (c == '-')
- s = -1, c = getchar_fast();
- while ('0' <= c && c <= '9')
- x = x * 10 + c - '0', c = getchar_fast();
- return s == 1 ? x : -x;
- }
- inline void readWord( char *s ) {
- int c = readChar();
- while (c >= 32 && c <= 126)
- *s++ = c, c = getchar_fast();
- *s = 0;
- }
- /** Write */
- static int write_pos = 0;
- static char write_buf[buf_size];
- inline void writeChar( int x ) {
- if (write_pos == buf_size)
- fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
- write_buf[write_pos++] = x;
- }
- inline void flush() {
- if (write_pos)
- fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
- }
- template <class T>
- inline void writeInt( T x ) {
- if (x < 0)
- writeChar('-'), x = -x;
- char s[24];
- int n = 0;
- while (x || !n)
- s[n++] = '0' + x % 10, x /= 10;
- while (n--)
- writeChar(s[n]);
- }
- inline void writeWord( const char *s ) {
- while (*s)
- writeChar(*s++);
- }
- } //namespace IO
- #define LP(i,s,f) for(sz_t i = s; i <= f; i++)
- #define FORN(i,n) for(sz_t i = 0; i < n; i++)
- #define BLP(i,s,f) for(sz_t i= s; i >= f; i--)
- #define PB push_back
- #define MP make_pair
- #define SZ(a) ((sz_t)(a.size()))
- #define ALL(c) c.begin(),c.end()
- #define FT first
- #define SD second
- typedef long long hash_t;
- typedef int sz_t;
- const hash_t P = 31;
- const hash_t M = 1000000000 + 7;
- const sz_t INF = 1000000000 + 7;
- hash_t MOD(hash_t val){
- if (val < 0){
- return val + M;
- } else {
- return val % M;
- }
- }
- class HashStr
- {
- private:
- char *ptr;
- sz_t size;
- vector<hash_t> *h, *deg;
- public:
- HashStr(char *raw_ptr, sz_t raw_size) : ptr(raw_ptr), size(raw_size){
- h = new vector<hash_t>;
- deg = new vector<hash_t>;
- h->reserve(size + 1);
- deg->reserve(size + 1);
- (*h)[0] = 0;
- (*deg)[0] = 1;
- FORN(i, size){
- (*deg)[i + 1] = MOD((*deg)[i] * P);
- (*h)[i + 1] = MOD(MOD((*h)[i] * P) + (ptr[i] - 'a' + 1));
- }
- };
- ~HashStr(){
- delete h;
- delete deg;
- };
- hash_t GetHash(sz_t l, sz_t r){
- return MOD((*h)[r + 1] - MOD((*h)[l] * (*deg)[r - l + 1]));
- };
- };
- class ZFuncStr
- {
- private:
- char *ptr;
- sz_t size;
- vector<sz_t>* z;
- public:
- ZFuncStr(char *raw_ptr, int raw_size) : ptr(raw_ptr), size(raw_size){
- z = new vector<sz_t>;
- z->assign(size, 0);
- sz_t l = 0, r = 0;
- (*z)[0] = 0;
- LP(i, 1, size - 1){
- if (i <= r){
- (*z)[i] = min(r - i + 1, (*z)[i - l]);
- }
- while(i + (*z)[i] < size && ptr[(*z)[i]] == ptr[i + (*z)[i]]){
- (*z)[i]++;
- }
- if (i + (*z)[i] - 1 > r){
- l = i;
- r = i + (*z)[i] - 1;
- }
- }
- };
- ~ZFuncStr(){
- delete z;
- };
- hash_t GetZFunc(int i){
- return (*z)[i];
- };
- };
- struct Vertex{
- static const sz_t k = 26;
- sz_t go[k], cnt;
- bool isTerm;
- Vertex(){
- fill(go, go + k, -1);
- cnt = 0;
- isTerm = false;
- }
- };
- vector<Vertex> bohr(1);
- const char e = 'a';
- void addString(char *str, sz_t len){
- sz_t v = 0;
- FORN(i, len){
- bohr[v].cnt++;
- sz_t c = str[i] - e;
- if (bohr[v].go[c] == -1){
- bohr[v].go[c] = SZ(bohr);
- bohr.PB(Vertex());
- }
- v = bohr[v].go[c];
- }
- bohr[v].cnt++;
- bohr[v].isTerm = true;
- }
- void writeKth(sz_t k){
- sz_t v = 0;
- while(k > 0){
- if (k == 1 && bohr[v].isTerm){
- k -= 1;
- }else {
- sz_t sk = sz_t(bohr[v].isTerm), i = -1;
- while(sk < k){
- i++;
- sz_t child = bohr[v].go[i];
- if (child != -1){
- sk += bohr[child].cnt;
- }
- }
- sk -= bohr[bohr[v].go[i]].cnt;
- IO::writeChar(char('a' + i));
- v = bohr[v].go[i];
- k -= sk;
- }
- }
- }
- sz_t n, x, l;
- char s[100000];
- int main(){
- #define filename "kthstr"
- assert(freopen(filename ".in", "r", stdin));
- assert(freopen(filename ".out", "w", stdout));
- n = IO::readInt();
- FORN(i, n){
- IO::readWord(s);
- if (s[0] >= '0' && s[0] <= '9'){
- x = atoi(s);
- writeKth(x);
- IO::ENDLINE;
- }else {
- l = strlen(s);
- addString(s, l);
- }
- }
- IO::flush();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement