Skip to content

Instantly share code, notes, and snippets.

@GnsP
Created May 26, 2015 15:54
Show Gist options
  • Select an option

  • Save GnsP/0a0f312c38d80ca9808e to your computer and use it in GitHub Desktop.

Select an option

Save GnsP/0a0f312c38d80ca9808e to your computer and use it in GitHub Desktop.
This is probably the most abstract piece of memory consuming shit I've ever written. And holy crap, it got accepted in the 1st attempt itself :o
#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <string>
#include <queue>
#include <deque>
#include <set>
#include <list>
#include <map>
#include <functional>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <stack>
#include <utility>
using namespace std;
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define p(n) printf("%d ",n)
#define pl(n) printf("%lld",n)
#define sf(n) scanf("%f",&n)
#define sd(n) scanf("%lf",&n)
#define ss(n) scanf("%s",n)
#define pf(n) printf("%f ",n)
#define pd(n) printf("%lf ",n)
#define ps(n) printf("%s ",n)
#define nl putchar('\n')
#define e1 first
#define e2 second
#define INF (int)1e9
#define MOD (int)(1e9+7)
#define LINF (ll)1e18
#define EPS 1e-11
const double PI = acos(-1.0)
#define pow2(n) (1<<(n))
#define pow2l(n) ((ll)1<<(n))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ABS(n) ((a)<0?-(a):(a))
#define MAXE(...) max_element(__VA_ARGS__)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define RNG(i,n) FOR(i,0,n)
#define REV(i,a,b) for(int i=a;i>=b;--i)
#define FORECH(v,c) for(typeof((c).begin()) v=(c).begin(); v!=(c).end(); ++v)
#define ALL(a) (a).begin(),(a).end()
#define LEN(a) ((int)(a.size()))
#define PB(x) push_back(x)
#define TIMES(x) while((x)--)
#define UPB upper_bound
#define LWB lower_bound
#define MP make_pair
#ifndef ONLINE_JUDGE
#define DEBUG__
#endif
#ifdef DEBUG__
#define derr(x) cerr<<"<"<<#x<<": "<<x<<">"<<endl
#define _DEBUG(...) __VA_ARGS__
;template<typename T> void debug(T a, T b){ for(;a!=b;++a) cerr<<*a<<" "; cerr<<endl; }
#else
#define derr(...)
#define _DEBUG(...)
;template<typename T> void debug(T a, T b){}
#endif
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef list<int> li;
typedef map<int,int> mii;
template<typename T> T modPow(T b, T e, T m=(ll)MOD){T res=1;while(e){if(!(e&0x1))res=(res*b)%m;e>>=1;b=(b*b)%m;}return res; }
template<typename T> T gcd (T u, T v){ return (u==0||v==0||u==v)?(u|v):((~u&1)?((v&1)?gcd(u>>1,v):gcd(u>>1,v>>1)<<1):((~v&1)?gcd(u,v>>1):((u>v)?gcd((u-v)>>1,v):gcd((v-u)>>1,u)))); }
template<typename T> T lcm (T a, T b){ return a*b/gcd(a,b); }
//#undef DEBUG__
/////////////////////////////// FAST IO ///////////////////////////////////////
#define gc getchar_unlocked
void si(int &n){
register int ch=gc();
int neg = 0;
n=0;
while((ch<48||ch>57) && ch!='-')ch=gc();
if(ch=='-'){ neg=1; ch=gc(); }
for(;ch>47 && ch<58; ch=gc()) n = (n<<1)+(n<<3)+ch-48;
if(neg)n=-n;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
int st[1000010][27];
char str[1005][1005];
int so[1000010];
int pr[1005];
int main(){
register int N,ch,z,len; si(N);
int ind, est=0, Q;
str[0][0]='N';str[0][1]='O';str[0][2]='\0';
RNG(i,1005)pr[i]=-INF-1;
FOR(i,1,N+1){
len=0;
ch=gc();
while((ch<'a' || ch>'z') && ch!='-') ch=gc();
while((ch>='a' && ch<='z')||ch=='-'){
str[i][len++]=ch;
ch=gc();
}
str[i][len]='\0';
si(pr[i]);
z=0;
RNG(j,len){
ind = (str[i][j] == '-')?26:str[i][j]-'a';
if(st[z][ind] == 0){
st[z][ind]=++est;
z = est;
}
else z=st[z][ind];
if(pr[so[z]]<pr[i])so[z]=i;
}
}
si(Q); TIMES(Q){
ch=gc();
z=0;
while((ch<'a' || ch>'z') && ch!='-') ch=gc();
while((ch>='a' && ch<='z')||ch=='-'){
ind = (ch == '-')?26:ch-'a';
z=st[z][ind];
ch=gc();
}
ps(str[so[z]]);nl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment