您的位置:首页 > 理论基础 > 计算机网络

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐