hdu 4964 Emmet()模拟
2015-09-20 14:06
337 查看
题目链接:hdu 4964 Emmet
题目大意: 给定语句,依照语法翻译并输出。
解题思路:用递归模拟文法分析,主要注意几点:
括号而且的情况:(fuck)(you)
括号嵌套的情况:((fuck.you))
优先输出id,然后是class(题目中有说)
乘法的部分:fuck*2>you*3 (每次运行fuck时。you的地方相同被运行了3次)
其它跑出例子基本没问题,详细看代码。
题目大意: 给定语句,依照语法翻译并输出。
解题思路:用递归模拟文法分析,主要注意几点:
括号而且的情况:(fuck)(you)
括号嵌套的情况:((fuck.you))
优先输出id,然后是class(题目中有说)
乘法的部分:fuck*2>you*3 (每次运行fuck时。you的地方相同被运行了3次)
其它跑出例子基本没问题,详细看代码。
#include <cstdio> #include <cstring> #include <string> #include <vector> #include <iostream> #include <algorithm> using namespace std; typedef vector<string> VS; const int manx = 1005; int mv, N; string str, word; bool isletter (char ch) { if (ch >= 'a' && ch <= 'z') return true; if (ch >= 'A' && ch <= 'Z') return true; if (ch >= '0' && ch <= '9') return true; if (ch == '_') return true; return false; } int changeTonumber () { int ret = 0; for (int i = 0; i < word.size(); i++) ret = ret * 10 + word[i] - '0'; return ret; } string getword () { string cur = ""; while (isletter(str[mv])) cur = cur + str[mv++]; mv--; return cur; } void gettoken () { if (mv == str.size()) { word = ""; return; } while (str[mv] == ' ') mv++; switch (str[mv]) { case '.': word = "."; break; case '#': word = "#"; break; case '>': word = ">"; break; case '(': word = "("; break; case ')': word = ")"; break; case '*': word = "*"; break; default: word = getword(); break; } mv++; } void putstring (string name, VS setid, VS setclass) { cout << "<" << name; if (setid.size() != 0) { cout << " id=\"" << setid[0]; for (int i = 1; i < setid.size(); i++) cout << " " << setid[i]; cout << "\""; } if (setclass.size() != 0) { cout << " class=\"" << setclass[0]; for (int i = 1; i < setclass.size(); i++) cout << " " << setclass[i]; cout << "\""; } cout << ">"; } void solve () { int ret = mv, x, y, ti = 1; string recstring = ""; gettoken(); while (word == "(") { solve(); // now word is ")"; gettoken(); // get a new token; } if (word == ">") { // after solve '()'; solve(); return; } if (word == "" || word == ")") return; string name; VS setclass, setid; while (true) { if (word == ".") { gettoken(); setclass.push_back(word); } else if (word == "#") { gettoken(); setid.push_back(word); } else if (word == ">") { putstring(name, setid, setclass); solve(); break; } else if (word == ")" || word == "") { putstring(name, setid, setclass); break; } else if (word == "*") { x = mv - 1; gettoken(); y = mv; ti = changeTonumber(); for (int i = x; i < y; i++) { recstring += str[i]; str[i] = ' '; } } else name = word; gettoken(); } cout << "</" << name << ">"; for (int i = 1; i < ti; i++) { mv = ret; solve(); } for (int i = 0; i < recstring.size(); i++) str[i+x] = recstring[i]; } int main () { int cas; cin >> cas; while (cas--) { cin >> str; mv = 0; solve(); cout << endl; } return 0; }
相关文章推荐
- 数据库范式——通俗易懂
- iOS开发 - NSURLSession简单介绍及POST、GET请求案例
- 超炫Android-ConvenientBanner轻松实现广告头效果
- 黑马程序员-------NSString 学习
- iOS Navigation+TabBar简单框架
- hdu 4734 F(x)
- 23设计模式之抽象工厂(Abstract Factory)
- 2015Android移动开发应用主流菜单模式
- 基于jQuery Circlr插件实现产品图片360度旋转
- android error while loading shared libraries: libz.so.
- JS学习总结之基础
- WatchOS 2 app 与 Iphone app 数据交互(二)连接状态
- 【wireshark】开发环境搭建
- 花生棒怎样连接使用
- 对git的认识
- PHP对象在内存堆栈中的分配
- 《九九乘法表》
- Ubuntu下配置samba服务器
- PowerDesigner如何将字段的注释显示出来
- java 静态导入