QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#72939 | #4379. Grammar | poi | AC ✓ | 71ms | 4304kb | C++ | 2.6kb | 2023-01-20 19:33:16 | 2023-01-20 19:33:17 |
Judging History
answer
#include "iostream"
#include "cstring"
#include "cstdio"
#include "algorithm"
#include "queue"
#include "vector"
#include "queue"
#include "stack"
#include "ctime"
#include "set"
#include "map"
#include "bitset"
using namespace std;
#define fi first
#define se second
#define vi vector<int>
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define mp make_pair
#define rep( i , a , b ) for( int i = (a) , i##end = b ; i <= i##end ; ++ i )
#define per( i , a , b ) for( int i = (a) , i##end = b ; i >= i##end ; -- i )
#define mem( a ) memset( a , 0 , sizeof (a) )
#define all( x ) x.begin() , x.end()
//#define int long long
typedef long long ll;
const int MAXN = 2e3 + 10;
const int P = 998244353;
int n , q;
struct tr {
int v; string S;
tr( ) = default;
tr( int l , string t ) : v(l) , S(t) {}
};
vector<tr> G[MAXN];
int loop[MAXN] , vis[MAXN] , cid , ins[MAXN];
ll le[MAXN];
void dfs( int u ) {
if( ins[u] ) {
loop[u] = 1; return;
} else if( vis[u] == cid ) {
return;
} else le[u] = 0;
vis[u] = cid , ins[u] = 1;
for( auto& t : G[u] ) {
le[u] += t.S.length();
if( t.v ) {
dfs( t.v );
if( !loop[t.v] ) le[u] += le[t.v];
loop[u] |= loop[t.v];
}
if( le[u] > 2e18 ) le[u] = 2e18;
}
ins[u] = 0;
}
ll plen[MAXN];
int flg = 0;
char que( int u , ll c ) {
if( plen[u] && !flg ) {
int l = plen[u] - c;
c = c % l , c = ( !c ? l : c ) , flg = 1;
}
plen[u] = c;
for( auto& t : G[u] ) {
if( c <= t.S.length() ) return t.S[c - 1];
c -= t.S.length();
if( t.v == 0 ) return 0;
if( loop[t.v] ) return que( t.v , c );
else {
if( c <= le[t.v] ) return que( t.v , c );
c -= le[t.v];
}
}
}
char ch[MAXN];
void solve() {
cin >> n >> q;
rep( i , 1 , n ) {
int x;
scanf("\n[%d]->",&x);
scanf("%s",ch + 1);
int l = strlen( ch + 1 ) , ot = 0;
G[x].eb( 0 , "" );
rep( i , 1 , l ) {
if( ch[i] == '[' ) {
++ i;
int ret = 0;
while( ch[i] >= '0' && ch[i] <= '9' ) ret = ret * 10 + ch[i] - '0' , ++ i;
G[x].back().v = ret;
G[x].eb( 0 , "" );
} else {
G[x].back().S.pb( ch[i] );
}
}
}
rep( i , 1 , n ) cid = i , dfs( i );
// cout << le[2] << endl;
rep( i , 1 , q ) {
int u;
ll l;
scanf("%d%lld",&u,&l);
flg = 0;
char as = que( u , l );
if( as ) putchar( as ) , puts("");
else puts("-1");
rep( i , 1 , n ) plen[i] = 0;
}
rep( i , 1 , n ) G[i].clear() , vis[i] = ins[i] = plen[i] = le[i] = loop[i] = 0;
}
signed main() {
// freopen("in","r",stdin);
// freopen("out","w",stdout);
int T;cin >> T;while( T-- ) solve();
// solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 71ms
memory: 4304kb
input:
10 1000 1000 [1]->ymkl[794]l[2]a[106]v[2][97]x[9]udiq[3]jrpt[265]l[111]w[43]jnb[4]dpjp[3][23]q[511]roml[49][20]j[34][4]qcgjv[443]qg[2][201]j[75]y[2]qi[51][49]y[536]vxdo[243]h[431]vp[470]i[154][30]uhvipimij[65]zoe[36]r [2]->gka[94][216][410]or[58]v[12][3]gf[20]u[328][115][71][79]j[239][102][3]y[46]mr...
output:
q c v t z n i t t j b c q b y -1 z t w c m l h n h -1 n c x i s v u o d d o x l n i v l o l o -1 k c l f f z q b d c x m i z -1 o y o r w e -1 f z j b j f h e r g i m n m l k f -1 r d p c s v q d z s q x l s f b -1 p -1 n m l -1 -1 c y p e n f s p b -1 c h c x s s t t r -1 a t l s t d d q m e -1 v t...
result:
ok 10000 lines