【模拟】 hdu4409 Family Name List

2 篇文章 0 订阅

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4409

题意:给你一个家族表,同时有3种操作,问你操作后的结果。

题解:模拟题,把3个操作处理好就行了。题目有个陷阱,比如a是b的父亲,b是c的父亲,当问你b和c的最近的共同父辈是谁的时候,答案是a而不是b。还有问最开头那个祖先的兄弟时要返回1。

代码:

#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
using namespace std;
#define M 30005
map<int,string> hasht;
map<string,int> hashx;
struct cmp//排序
{
    bool operator()(const int &a, const int &b)
    {
        return hasht.find(a)->second<hasht.find(b)->second;
    }
};
struct point
{
    int pre;//父结点的位置
    set<int,cmp> son;//子结点的位置
    string name;//当前结点的名字
}node[M];
struct px
{
    int idx,len;//结点的位置,结点的辈分
} temp;
int cnt;
stack<struct px> father;
void dfs(int idx,int len)//递归输出族谱
{
    for(int i=0; i<len; ++i) printf(".");
    printf("%s\n",node[idx].name.c_str());
    for(set<int,cmp>::iterator it=node[idx].son.begin(); it!=node[idx].son.end(); ++it)
        dfs(*it,len+1);
}
void out_all()//Print the family list
{
    dfs(0,0);
}
void out_child()//Print out how many brothers does "name" have, including "name" himself.
{
    char s[70];
    scanf("%s",s);
    if(hashx.count(string(s))==0)
    {
        printf("0\n");
        return;
    }
    if(string(s)==node[0].name)
    {
        printf("1\n");
        return;
    }
    int idx=hashx.find(string(s))->second;
    printf("%d\n",node[node[idx].pre].son.size());
}
void out_pre()//Print out the closest common ancestor of "name1" and "name2".
{
    char a[70],b[70];
    scanf("%s%s",a,b);
    set<int> ax;
    int ida=hashx.find(string(a))->second;
    int idb=hashx.find(string(b))->second;
    for(; ida!=-1;)
    {
        ida=node[ida].pre;
        ax.insert(ida);
    }
    for(; idb!=-1;)
    {
        idb=node[idb].pre;
        if(ax.count(idb)!=0) break;
    }
    printf("%s\n",node[idb].name.c_str());
}
int main()
{
    int n,m,pre;
    char s[70],c,ord[10];
    string sx;
    for(; scanf("%d",&n),n;)
    {
        for(int i=0; i<=n; ++i)
        {
            node[i].pre=-1;
            node[i].son.clear();
        }
        hashx.clear();
        hasht.clear();
        for(; !father.empty(); father.pop());
        cnt=0;
        temp.idx=0;
        temp.len=0;
        scanf("%s",s);
        sx=string(s);
        node[cnt++].name=sx;
        hashx.insert(make_pair(sx,0));
        hasht.insert(make_pair(0,sx));
        father.push(temp);
        for(int i=1; i<n; ++i)
        {
            for(; getchar()!='.';);
            int ll=1;
            for(; (c=getchar())=='.'; ++ll);
            s[0]=c;
			int llt;
            for(llt=1;(s[llt]=getchar())!='\n';++llt);
			s[llt]='\0';
            temp.len=ll;
            temp.idx=cnt;
            for(;;)
            {
                struct px tx=father.top();
                if(tx.len>=temp.len)
                    father.pop();
                else
                    break;
            }
            sx=string(s);
            int tx=father.top().idx;
            node[cnt].pre=tx;
            node[cnt].name=sx;
            hasht.insert(make_pair(cnt,sx));
            hashx.insert(make_pair(sx,cnt));
            node[tx].son.insert(cnt);
            cnt++;
            father.push(temp);
        }
        scanf("%d",&m);
        for(; m--;)//处理查询
        {
            scanf("%s",ord);
            if(ord[0]=='L')
                out_all();
            else if(ord[0]=='b')
                out_child();
            else if(ord[0]=='c')
                out_pre();
        }
    }
    return 0;
}
来源: http://blog.csdn.net/acm_ted/article/details/8007857

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值