题意: 给一个字符串,表示一颗树,要求你把它整理出来,节点从1开始编号,还要输出树边。

解法: 模拟即可。因为由括号,所以可以递归地求,用map存对应关系,np存ind->name的映射,每进入一层括号,使father = now, 遇到右括号')',则father = fa[father],用vector存每个节点的子节点,然后最后dfs输出即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
using namespace std;
#define N 50017 string ss,tmp,S;
string np[N];
map<string,int> mp;
int fa[N],father,ind;
vector<int> G[N]; void Go(int u,int v,int father)
{
tmp = "";
int j;
for(int i=u;i<v;i++)
{
if(ss[i] >= 'a' && ss[i] <= 'z')
tmp += ss[i];
else if(ss[i] == '(' || ss[i] == ',' || ss[i] == ')')
{
if(tmp == "") continue;
mp[tmp] = ++ind;
np[ind] = tmp;
tmp = "";
fa[ind] = father;
G[father].push_back(ind);
if(ss[i] == '(')
{
int cnt = ;
for(j=i+;j<v;j++)
{
if(ss[j] == '(') cnt++;
else if(ss[j] == ')')
{
cnt--;
if(cnt == ) break;
}
}
Go(i+,j,ind);
i = j;
}
else if(ss[i] == ')')
father = fa[father];
}
}
if(tmp != "")
{
mp[tmp] = ++ind;
np[ind] = tmp;
tmp = "";
fa[ind] = father;
G[father].push_back(ind);
}
} void dfs(int u)
{
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
printf("%d %d\n",u,v);
dfs(v);
printf("%d %d\n",v,u);
}
} int main()
{
int t,i,j,len;
scanf("%d",&t);
while(t--)
{
mp.clear();
for(i=;i<=;i++)
G[i].clear();
cin>>ss;
len = ss.length();
father = ;
ind = ;
Go(,len,);
printf("%d\n",ind);
for(i=;i<=ind;i++)
cout<<np[i]<<endl;
dfs();
puts("");
}
return ;
}

HDU 4041 Eliminate Witches! --模拟的更多相关文章

  1. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  2. HDU 4115 Eliminate the Conflict(2-sat)

    HDU 4115 Eliminate the Conflict pid=4115">题目链接 题意:Alice和Bob这对狗男女在玩剪刀石头布.已知Bob每轮要出什么,然后Bob给Al ...

  3. HDU 5510---Bazinga(指针模拟)

    题目链接 http://acm.hdu.edu.cn/search.php?action=listproblem Problem Description Ladies and gentlemen, p ...

  4. HDU 5047 Sawtooth(大数模拟)上海赛区网赛1006

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5047 解题报告:问一个“M”型可以把一个矩形的平面最多分割成多少块. 输入是有n个“M",现 ...

  5. HDU 5965 扫雷 【模拟】 (2016年中国大学生程序设计竞赛(合肥))

    扫雷 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submissi ...

  6. HDU 5935 Car 【模拟】 (2016年中国大学生程序设计竞赛(杭州))

    Car Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  7. HDU 5912 Fraction 【模拟】 (2016中国大学生程序设计竞赛(长春))

    Fraction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  8. hdu 4831 Scenic Popularity(模拟)

    pid=4831" style="font-weight:normal">题目链接:hdu 4831 Scenic Popularity 题目大意:略. 解题思路: ...

  9. HDU 5538 House Building(模拟——思维)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5538 Problem Description Have you ever played the vi ...

随机推荐

  1. No.011:Container With Most Water

    问题: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  2. hibernate4整合spring3事务问题

    本文是作者在对hibernate4+spring3+struts2整合中遇到的一个问题.对s2sh进行了基本的整合搭建以后,就是对事务的控制管理,将hibernate的事务交由spring管理.根据网 ...

  3. Ansible用于网络设备管理 part 3 使用NAPALM成品库

    闲话 经过了这俩月的闲暇时间的瞎逛和瞎琢磨,我发现NAPALM是一条路,NAPALM是由帅哥David Barroso和美女Elisa Jasinska创建的一个项目,都是颜值高的技术牛人啊,真是不给 ...

  4. css超出2行部分省略号...

    今天做东西,遇到了这个问题,百度后总结得到了这个结果. 首先,要知道css的三条属性. overflow:hidden; //超出的文本隐藏 text-overflow:ellipsis; //溢出用 ...

  5. 移动,企业社交(sharepoint2013)--jindahao(金大昊)

    MobileIncreasingly, a major component of sharing and collaborating involves mobile access. SharePoin ...

  6. windows 80端口被占用的解决方法

    参考文献: 文献1: http://wenku.baidu.com/view/af4681bcfd0a79563c1e7289.html 文献2: http://www.2cto.com/os/201 ...

  7. Core Data的简单用法

    #import "ViewController.h" // 第一步:引入头文件AppDelegate #import "AppDelegate.h" #impo ...

  8. IOS的UI基础02

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  9. Cocos2d入门--3--向量的应用

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  10. SQL Server智能感知如何更新

    经常用sql server发现一个问题,比如说我刚刚添加个表或者字段,这时候在sqlserver里面写sql语句时,没有智能提示,这个问题我以前一直不是太注意.今天好好找了下解决方法,这里做下分享. ...