ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 The Book List
2016-09-25 14:43
405 查看
#1383 : The Book List
题意:给你一些书及书归类,叫你画出一棵书的整理树分析:模拟
样例输入
B/A B/A B/B 0 A1/B1/B32/B7 A1/B/B2/B4/C5 A1/B1/B2/B6/C5 A1/B1/B2/B5 A1/B1/B2/B1 A1/B3/B2 A3/B1 A0/A1 0
样例输出
Case 1: B A B Case 2: A0 A1 A1 B B2 B4 C5 B1 B2 B6 C5 B1 B5 B32 B7 B3 B2 A3 B1
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <algorithm> #include <cstdlib> #include <vector> #include <set> #include <map> #include <iterator> using namespace std; const int maxn = 320; int tot,cas; struct Node //节点 { string str; int o; int ran; }; vector<Node> g[maxn]; // 树 ,记录每个节点的儿子Node map<string,int> mp[maxn]; //保存树,mp【u】保存儿子的字符串,及节点编号 map<string,int> my[maxn];//保存每个非叶子节点的,字符串 bool flag[maxn];//标记是否为叶子节点 void dfs(int u,int step); //用于输出 bool cmp(const Node& a,const Node& b); void init(); void solve() { string ss,st;//st 表示截取的类名,ss表示输入每行的总信息 string::iterator it,pre; map<string,int>::iterator mpper,mper; //mp,my的指针 int fath = 0; //记录父亲节点的编号 Node node; while(getline(cin,ss)) { init(); if(ss!="0") do{ fath = 0; for ( it=ss.begin(),pre=ss.begin(); ; ++it) { if(it==ss.end()) { st.assign(pre,it); pre = it; mpper = mp[fath].find(st);//看字符串st在mp[fath]是否找得到 mper = my[fath].find(st);//看字符串st在my[fath]是否找得到 if(mpper==mp[fath].end()) // { int sz = g[fath].size();tot++; mp[fath].insert(make_pair(st,tot)); node.o = tot; node.ran = sz; node.str = st; g[fath].push_back(node); fath = tot; flag[tot] = true; break; } else if(mper==my[fath].end()) { break; } else{ if(flag[mpper->second]) break; //之前是否有这本书,有的话退出 mp[fath].erase(mpper); int sz = g[fath].size();tot++; mp[fath].insert(make_pair(st,tot)); node.o = tot; node.ran = sz; node.str = st; g[fath].push_back(node); fath = tot; flag[tot] = true; } break; } if(*it=='/') { st.assign(pre,it); pre = it+1; mpper = my[fath].find(st); if(mpper == my[fath].end()) { int sz = g[fath].size();tot++; mp[fath].insert(make_pair(st,tot)); my[fath].insert(make_pair(st,tot)); node.o = tot; node.ran = sz; node.str = st; g[fath].push_back(node); fath = tot; } else { int d = (*mpper).second; fath = d; } } } }while(getline(cin,ss)&&ss!="0"); cas ++; printf("Case %d:\n",cas); dfs(0,0); } } bool cmp(const Node& a,const Node& b) { return a.str <= b.str; } void dfs(int u,int step) { string str; str.assign(4*step,' '); int sz = g[u].size(); if(sz==0) { return ; } int v; sort(g[u].begin(),g[u].end(),cmp); for(int i=0;i<sz;i++) { v = g[u][i].o; if(flag[v]) continue; cout << str; cout << g[u][i].str << endl;; dfs(v,step +1); } for(int i=0;i<sz;i++) { v = g[u][i].o; if(!flag[v]) continue; cout << str; cout << g[u][i].str << endl;; dfs(v,step +1); } } void init() { tot = 0; for(int i=0;i<305;i++) { mp[i].clear(); g[i].clear(); my[i].clear(); } memset(flag,false,sizeof(flag)); } int main() { cas = 0; init(); //read(); solve(); return 0; }
相关文章推荐
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 The Book List
- Booklist——ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 A
- 北京赛区(2016)网络赛 A题 The Book List
- hihoCoder 1227 The Cats' Feeding Spots(暴力)——ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛
- hihoCoder 1389 Sewage Treatment 【二分+网络流+优化】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
- hihoCoder 1227 The Cats' Feeding Spots && ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛
- hihoCoder 1392 War Chess 【模拟】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 A
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 A Simple Job
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 C.A Simple Job map<string, map<string, int> > 、字符串单词词组的处理
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 - C A Simple Job (模拟)(multiset)
- hihoCoder 1391 Countries 【预处理+排序+堆】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 I
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 F. Periodic Signal(FFT 优化乘法)
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 I
- hihoCoder 1391 Countries 【预处理+排序+堆】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
- hihoCoder 1391 Countries 【预处理+排序+堆】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
- hihoCoder 1389 Sewage Treatment 【二分+网络流+优化】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 C (map模拟)
- hihoCoder 1392 War Chess 【模拟】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)