HDU 4041 Eliminate Witches! --模拟

题意: 给一个字符串,表示一颗树,要求你把它整理出来,节点从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 = 1;
                for(j=i+1;j<v;j++)
                {
                    if(ss[j] == ‘(‘) cnt++;
                    else if(ss[j] == ‘)‘)
                    {
                        cnt--;
                        if(cnt == 0) break;
                    }
                }
                Go(i+1,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=0;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=0;i<=50000;i++)
            G[i].clear();
        cin>>ss;
        len = ss.length();
        father = 0;
        ind = 0;
        Go(0,len,0);
        printf("%d\n",ind);
        for(i=1;i<=ind;i++)
            cout<<np[i]<<endl;
        dfs(1);
        puts("");
    }
    return 0;
}

时间: 2024-10-11 05:45:38

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

HDU 4041 Eliminate Witches! (栈的模拟)

Eliminate Witches! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1234    Accepted Submission(s): 461 Problem Description Kaname Madoka is a Magical Girl(Mahou Shoujo/Puella Magi). The duty of

HDU ACM 4041 Eliminate Witches! 队列和栈模拟DFS

分析:直接模拟即可,这里用队列记录访问过的点,栈记录父节点.另外要注意的是在strlen(str)计算字符串的时候要预先计算出来保存在变量中,for直接用,如果for循环直接调用strlen,那么每次都会重新计算,该題字符串的数据量很大,就会存在大量的无用计算,还导致了一次TLE,唉!以前没注意到这里. #include<iostream> #include<vector> #include<queue> #include<stack> using name

HDU 4043 Eliminate Witches! (求概率推公式 + 大数)

FXTZ II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 525    Accepted Submission(s): 276 Problem Description Cirno is playing a fighting game called "FXTZ" with Sanae. Sanae is a ChuShou

hdu 1175 连连看(模拟循环队列)

连连看 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18149    Accepted Submission(s): 4741 Problem Description "连连看"相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以通过一条线连起来(这条

HDU 4608 I-number--简单模拟

I-number Time Limit: 5000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 The I-number of x is defined to be an integer y, which satisfied the the conditions below: 1.  y>x; 2.  the sum of each digit of y(under base 10) is the multiple of 10; 3.  among all

hdu 4831 Scenic Popularity(模拟)

题目链接:hdu 4831 Scenic Popularity 题目大意:略. 解题思路:对于休闲区g[i][0]和g[i][1]记录的是最近的两个景点的id(只有一个最近的话g[i][1]为0),对于景点来说,g[i][0]为-1(表示该id对应的是景点),g[i][1]为该景点的热度值.主要就是模拟,注意一些细节就可以了. #include <cstdio> #include <cstring> #include <cstdlib> #include <alg

hdu 4115 Eliminate the Conflict

Description Conflicts are everywhere in the world, from the young to the elderly, from families to countries. Conflicts cause quarrels, fights or even wars. How wonderful the world will be if all conflicts can be eliminated.Edward contributes his lif

hdu 3125 Slash(模拟)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3125 Problem Description The American English slash (/) is a punctuation mark. In the early modern period, in the Fraktur script, which was widespread through Europe in the Middle Ages, one slash(/) repr

hdu 4858 项目管理(vector模拟)

# include <stdio.h> # include <algorithm> # include <string.h> # include <vector> # define N 100005 using namespace std; vector<int>g[N]; int node[N]; int slove(int x) { int sum=0,i; for(i=0;i<g[x].size();i++) { sum+=node[