ZOJ - 3480 Duck Typing 模拟

代码很清晰的说。 比赛那天对string不够熟,而且关系表示时想歪了 = =` 。

其实把map看成二维数组就OK了, 当然map更强大。

我先找着网上一个程序打一遍,然后自己在实现一遍。(它用的都是MAP, 我把集合表示这块改SET了)。

学到了不少的知识, 下面汇总一下:

(下面的string表示string类)

 

string.erase(int i) 删除从i开始到最后的子字符串

string.erase(string::iterator it) 在删除中删除it指针所指的字符(it 其实就是char* 类型,即unsigned int,不知道对不对,有待查证

string.erase(int a, int b) 删除字符串中位置从a到b的子串

 

string.substr(int i) 从i位置开始到最后的子串

string.substr(int a, int b) 从a位置开始到b的子串(不包括b)


string.find(char c)函数返回第一个‘c’ 出现的位置(unsigned int类型)

 

#include <cstdio> #include <iostream> #include <map> #include <set> #include <string> using namespace std; int main(int argc, char* argv[]) { map<string, string> father; set<string, int> method; set<string, int> defclass; int td, i; string cmd, me, fa, func; cin >> td; while(td > 0) { cin >> cmd; if(cmd == "begin") { father.clear(); method.clear(); defclass.clear(); } else if(cmd == "end") { --td;; cout << "/n"; } else if(cmd == "class") { cin >> me; if((i=me.find(":")) != string::npos) { fa = me.substr(i+1); me.erase(i); if((defclass.find(me) != defclass.end()) || defclass.find(fa) == defclass.end()) { cout << "oops!/n"; } else { defclass.insert(me); father[me] = fa; cout << "class" << me << ":" << fa << "/n"; } } else { if(defclass.find(me) != defclass.end()) { cout<<"oops!/n"; } else { defclass.insert(me); cout << "class" << me << "/n"; } } } else if(cmd == "def") { cin >> func; me = func.substr(0, func.find('.')); //去掉第一个 if(defclass.find(me) == defclass.end()) { cout << "oops!/n"; } else if(method.find(func) != method.end()) { cout<< "redef" << func << "/n"; } else { method.insert(func); cout<< "def" << func << "/n"; } } else if(cmd == "undef") { cin >> func; me = func.substr(0, func.find('.')); if((defclass.find(me) == defclass.end()) || (method.find(func) == method.end())) { cout << "oops!/n"; } else { method.erase(func);//直接删除字符串吗? cout << "undef" << func << "/n"; } } else if(cmd == "call") { cin >> func; me = func.substr(0, func.find('.')); func.erase(0, func.find('.')+1); while((!me.empty()) && (method.find(me+"."+func) == method.end())) { me = father[me]; } if(!me.empty()) { cout << "invoke" << me+"."+func << "/n"; } else { cout << "oops!/n"; } } } return 0; } 

你可能感兴趣的:(ZOJ - 3480 Duck Typing 模拟)