/*
以下は正しい構造である
<a>ruby<b>php</b>python</a><a><b></b></a><a></a>
お題
与えられた文字列が正しい構造であれば1、正しくなければ0を出力しなさい
<a>ruby<b>php</b>python</a><a></b></a><a></a>
<a>ruby<b>php</b>python</a><a><b></a><a></a>
<a>ruby<b>php</b>python</a><a><b></b></a><a>
vvvc<a>ruby<b>php</b>python</a><a><b></b></a><a></a>
*/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
typedef enum { Error='e', First='F', Text='T', Left='L', Right='R', EndTag='E' } Mode;
static void
parse_a(char** src, Mode* m);
static void
parse_b(char** src, Mode* m);
static void
parse_a(char** src, Mode* m) {
char* p = *src;
for (; *p; ++p) {
char ch = *p;
switch (*m) {
default: case Error: *src = p; return; break;
case Right:
else if (ch == '<') *m=Left;
else if (ch == '>') *m=Right;
else *m=Error;
break;
case Text:
else if (ch == '<') *m=Left;
else *m=Error;
break;
case Left:
if (ch == '/') *m=EndTag;
else if (ch == 'b') { ++p; *m=Right; parse_b(&p, m); }
else *m = Error;
break;
case EndTag:
if (ch == 'a') { *src = ++p; *m=Right; return; }
else *m = Error;
break;
}
}
*m=Error;
}
static void
parse_b(char** src, Mode* m) {
char* p = *src;
for (; *p; ++p) {
char ch = *p;
switch (*m) {
default: case Error: *src=p; return; break;
case Right:
if (ch == '>') *m=Right;
else if (ch == '<') *m=Left;
else *m=Error;
break;
case Text:
else if (ch == '<') *m=Left;
else *m=Error;
break;
case Left:
if (ch == '/') *m=EndTag;
else *m = Error;
break;
case EndTag:
if (ch == 'b') { *src = ++p; *m=Right; return; }
else *m=Error;
break;
}
}
*m=Error;
}
static int
parse(char* p) {
Mode m = First;
for (; *p; ++p) {
char ch = *p;
switch (m) {
default: case Error: return 0; break;
case First:
else if (ch == '<') m = Left;
else m = Error;
break;
case Text:
else if (ch == '<') m = Left;
else m = Error;
break;
case Left:
if (ch == 'a') {++p; m = Right; parse_a(&p, &m); }
else if (ch == 'b') {++p; m = Right; parse_b(&p, &m); }
else m = Error;
break;
case Right:
else if (ch == '<') m=Left;
else m=Error;
break;
}
}
if (m == Error)
return 0;
return 1;
}
int
main(void) {
char buf[1024];
while (fgets(buf
, sizeof buf
, stdin
)) { if (buf[len-1] == '\n')
buf[len-1] = '\0';
printf("%d '%s'\n", parse
(buf
), buf
); }
return 0;
}
LyoK5Lul5LiL44Gv5q2j44GX44GE5qeL6YCg44Gn44GC44KLCjxhPnJ1Ynk8Yj5waHA8L2I+cHl0aG9uPC9hPjxhPjxiPjwvYj48L2E+PGE+PC9hPgoK44GK6aGMCuS4juOBiOOCieOCjOOBn+aWh+Wtl+WIl+OBjOato+OBl+OBhOani+mAoOOBp+OBguOCjOOBsDHjgIHmraPjgZfjgY/jgarjgZHjgozjgbAw44KS5Ye65Yqb44GX44Gq44GV44GECjxhPnJ1Ynk8Yj5waHA8L2I+cHl0aG9uPC9hPjxhPjwvYj48L2E+PGE+PC9hPgo8YT5ydWJ5PGI+cGhwPC9iPnB5dGhvbjwvYT48YT48Yj48L2E+PGE+PC9hPgo8YT5ydWJ5PGI+cGhwPC9iPnB5dGhvbjwvYT48YT48Yj48L2I+PC9hPjxhPgp2dnZjPGE+cnVieTxiPnBocDwvYj5weXRob248L2E+PGE+PGI+PC9iPjwvYT48YT48L2E+IAoqLwojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN0cmluZy5oPgojaW5jbHVkZSA8c3RkYm9vbC5oPgojaW5jbHVkZSA8Y3R5cGUuaD4KCnR5cGVkZWYgZW51bSB7IEVycm9yPSdlJywgRmlyc3Q9J0YnLCBUZXh0PSdUJywgTGVmdD0nTCcsIFJpZ2h0PSdSJywgRW5kVGFnPSdFJyB9IE1vZGU7CgpzdGF0aWMgdm9pZApwYXJzZV9hKGNoYXIqKiBzcmMsIE1vZGUqIG0pOwoKc3RhdGljIHZvaWQKcGFyc2VfYihjaGFyKiogc3JjLCBNb2RlKiBtKTsKCnN0YXRpYyB2b2lkCnBhcnNlX2EoY2hhcioqIH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