AOJ 2587 Broken Cipher Generator
以下のBNFに従ってparserを書く。
・<Cipher>:=(<String>)+ ・<String>:='[' + <Cipher> + ']' | <Letter> ・<Letter>:=('+'|'-')* + <Alphabet>
但し、'?'を含む
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<(int)(n);i++) string expr_string(); char expr_letter(); int c; string s; string expr_cipher(){ string res=""; while(c!=s.length() && s[c]!=']'){ res+=expr_string(); } return res; } string expr_string(){ string res=""; char p=s[c]; if(p=='['){ c++; res=expr_cipher(); reverse(res.begin(),res.end()); c++; }else{ res+=expr_letter(); } return res; } char expr_letter(){ int cnt=0; while(s[c]=='+' || s[c]=='-'){ if(s[c]=='+') cnt++; else cnt--; c++; } if(s[c]=='?'){ c++; return 'A'; } char res='A'+(s[c]-'A'+cnt+130)%26; c++; return res; } int main(){ while(cin >> s && s!="."){ c=0; cout << expr_cipher() << endl; } return 0; }