-
Notifications
You must be signed in to change notification settings - Fork 11
/
SPOJ13384.cc
45 lines (39 loc) · 989 Bytes
/
SPOJ13384.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// SPOJ 13384: Nice Binary Trees
// http://www.spoj.cmm/problems/NICEBTRE/
//
// Solution: Parsing
//
// Knack of the string parsing:
// a parser returns a value and last+1 position.
// i.e., f(s) = (x,t) iff parse [s,t) == x
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
using namespace std;
#define ALL(c) c.begin(), c.end()
#define FOR(i,c) for(typeof(c.begin())i=c.begin();i!=c.end();++i)
#define REP(i,n) for(int i=0;i<n;++i)
#define fst first
#define snd second
pair<int,char*> solve(char *s) { // i からよむ
if (s[0] == 'l') {
return make_pair(0, s+1);
} else {
pair<int,char*> l = solve(s+1);
pair<int,char*> r = solve(l.snd);
r.fst = max(r.fst, l.fst) + 1;
return r;
}
}
int main() {
int T; scanf("%d", &T);
while (T--) {
char s[20000];
scanf("%s", s);
printf("%d\n", solve(s).fst);
}
}