栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1674 Family Tree

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1674 Family Tree

#include <iostream>#include <string.h>#include <stdio.h>#include <map>#include <memory.h>using namespace std;const int maxn = 1000 + 10;int layer[maxn];int parent[maxn];map<string,int> mhash;int num = 0;void solve(int i){    string s1;    getline(cin,s1,'n');    int tmp = s1.find_last_of(' ',s1.size()) - s1.find_first_of(' ',0) + 1;    if(0 !=i)        s1.erase(0,tmp);    mhash[s1] = num++;    layer[num - 1] = tmp;    if(0==i)        layer[num - 1] = 0;    int pos = num - 2;    while(layer[num - 1] <= layer[pos] && pos >= 0)        pos--;    if(pos >= 0)        parent[num - 1] = pos;    else        parent[num - 1] = -1;}bool judge(string s1,string s2,string s3){    int n1 = mhash[s1];    int n2 = mhash[s2];    if("child"==s3)    {        return parent[n1]== n2 ? true : false;    }    else if("parent"== s3)    {        return parent[n2]==n1 ? true : false;    }    else if("sibling"==s3)    {        return parent[n1]==parent[n2] ? true : false;    }    else if("descendant"==s3)    {        while(n1 >= 0)        { n1 = parent[n1]; if(n1 == n2)     break;        }        if(n1 >= 0) return true;        return false;    }    else     {        while(n2 >= 0)        { n2 = parent[n2]; if(n1==n2)     break;        }        if(n2 >= 0) return true;        return false;    }}int main(){    int n,m;    cin>>n>>m;    while(n || m)    {        getchar();        memset(layer,-1,sizeof(layer));        memset(parent,-1,sizeof(parent));        mhash.clear();        for(int i = 0;i < n;i++) solve(i);        string s1,s2,s3;        for(int i = 0;i < m;i++)        { cin>>s1; cin>>s3>>s3>>s3; cin>>s2>>s2; s2.erase(s2.end() - 1); if(judge(s1,s2,s3))     printf("Truen"); else     printf("Falsen");        }        printf("n");        cin>>n>>m;    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/375497.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号