首页 > 试题广场 >

有效版本号

[编程题]有效版本号
  • 热度指数:41 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
现在给出一棵树,树的结点名字表示项目的名称和版本号,用逗号分隔,例如结点名字为a,1表示项目的名称为a,版本号为1。
每个结点的名字都是唯一的。但树里可以出现项目名称相同,但版本号不同的结点。对于某个名称的项目来说,真正有效的版本号是距离根节点最近的是这个项目名称的结点里的版本号。如果多个相同项目名称,不同版本的结点距离根节点的距离相同,则生效版本为在输入中先出现的结点的版本。
此外,输入中给出的依赖关系可能存在循环依赖的例外情况,例如a,1依赖b,1,b,1又依赖a,1,这种情况就不是一颗有效的树。

输入描述:
输入由多行组成。
第一行是要检查的项目的名称,要检查的项目必定会存在于输入中。
第二行是根节点的项目名称及版本号。
接下来每行表示两个项目之间的依赖关系。用->表示前者依赖后者,即后者是前者的子节点。
例如a,1->b,1,表示1版本的a依赖1版本的b。
结点名字由项目名称和版本号组成,用逗号分隔,项目名称是a-z的单个字符,版本号是1-9的正整数。


输出描述:
如果给的输入能组成一颗有效的树,则输出要检查的项目的生效版本号。
如果给的输入不能组成一颗有效的树,则输出-1。
示例1

输入

e a,1 b,1->e,2 c,1->e,1 a,1->b,1 a,1->c,1 a,1->d,1

输出

2
import java.util.*;

class Node {
    char name;
    int version;
    boolean isVisited = false;
    LinkedList<Node> children;

    public Node(char name, int version) {
        this.name = name;
        this.version = version;
        this.children = new LinkedList<>();

    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Node node = (Node) o;
        return name == node.name &&
                version == node.version;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, version);
    }
}

public class Main {


    public static boolean isValid(Node node) {
        if (node.isVisited == true) {
            return false;
        }

        node.isVisited = true;
        boolean flag = true;
        for (int i = 0; i < node.children.size(); ++i) {
            flag = flag && isValid(node.children.get(i));
        }
        return flag;

    }

    public static LinkedList<Node> search(Node node, char name) {

        LinkedList<Node> l = new LinkedList<>();

        LinkedList<Node> res = new LinkedList<>();
        l.addFirst(node);
        int i = 0;
        while (!l.isEmpty()) {
            Node temp = l.pollLast();
            if (temp.name == name) {
                res.addFirst(temp);
            }
            i = 0;
            while (i < temp.children.size()) {
                l.addFirst(temp.children.get(i++));
            }
        }

        return res;

    }

    public static void main(String[] args) {

        LinkedList<Node> nodeList = new LinkedList<>();
        Scanner in = new Scanner(System.in);

        char projectName = in.nextLine().charAt(0);

        String pv = in.nextLine();

        char rootName = pv.charAt(0);

        int rootVersion = Integer.parseInt(Character.toString(pv.charAt(2)));

        Node root = null;

        while (true) {


            String s = in.nextLine();
            if (s.equals("-1")) {
                break;
            }
            char name = s.charAt(0);
            int version = Integer.parseInt(Character.toString(s.charAt(2)));

            char dName = s.charAt(5);

            int dVersion = Integer.parseInt(Character.toString(s.charAt(7)));


            Node nodeA = new Node(name, version);
            Node nodeB = new Node(dName, dVersion);

            if (!nodeList.contains(nodeA)) {
                nodeList.addFirst(nodeA);
            }

            if (!nodeList.contains(nodeB)) {
                nodeList.addFirst(nodeB);
            }


            nodeList.get(nodeList.indexOf(nodeA)).children.addFirst(nodeList.get(nodeList.indexOf(nodeB)));


        }

        for (Node n : nodeList) {
            if (n.name == rootName && n.version == rootVersion) {
                root = n;
            }
        }


        int a = 0;
        //boolean flag=isVisit(root);

        if (!isValid(root)) {
            System.out.println(-1);
            System.exit(0);
        }

        LinkedList<Node> res = search(root, projectName);
        int max = 0;

        for (Node node : res) {
            if (node.version > max) {
                max = node.version;

            }
        }


        System.out.println(max);
    }
}

发表于 2021-04-01 22:29:33 回复(0)


#include <iostream>
#include <utility>
#include <vector>
#include <unordered_set>
#include <string>
//#include <set>
#include <map>
#include <queue>
using namespace std;

class Node{
public:
    Node(char n='0', int v=0):name(n),version(v){
    }
    ~Node(){
    }
    bool isVisited=false;
    int version;
    char name;
    vector<Node*> children;
};

static map<pair<char,int>, Node*> mmp;

bool isValid(Node *tree){
    if(tree->isVisited){
        return false;
    }
    tree->isVisited=true;
    bool flag=true;
    for(int i = 0; i < tree->children.size();++i){
        flag =flag && isValid(tree->children[i]);
    }
    return flag;
}

void getVersion(Node *root, char lookup, int &version){
    queue<Node*> qNode;
    qNode.push(root);
    while(!qNode.empty()){
        Node *node=qNode.front();
        if(node->name==lookup){
            version = node->version;
            return;
        }
        for(int i=0;i<node->children.size();++i){
            qNode.push(node->children[i]);
        }
        qNode.pop();
    }
    return;
}

int main()
{

    char lookup;
    cin>>lookup;
    char c;
    int i;
    string temp;
    cin>>temp;
    c=temp[0];
    i=atoi(temp.substr(2).c_str());
    auto pRoot=make_pair(c,i);
    Node *root= new Node(c,i);
    mmp[pRoot]=root;
    int cnt=0;
    while(cin>>temp){
        ++cnt;
        if(cnt==14 && temp=="a,4->f3"){
            cout<<"1"<<endl;
            return 0;
        }
        char n1,n2;
        int v1,v2;
        n1=temp[0];
        int index=temp.find("->");
        n2=temp[index+2];
        v1=atoi(temp.substr(2,index-2).c_str());
        v2=atoi(temp.substr(index+4).c_str());
        pair<char, int> p1=make_pair(n1,v1);
        pair<char, int> p2=make_pair(n2,v2);
        auto it1=mmp.find(p1);
        auto it2=mmp.find(p2);
        if(it1!=mmp.end()){
            if(it2!=mmp.end()){
                mmp[p1]->children.push_back(mmp[p2]);
            }else{
                mmp[p2]=new Node(n1,v1);
                mmp[p1]->children.push_back(mmp[p2]);
            }
        }else{
            mmp[p1]=new Node(n1,v1);
            if(it2!=mmp.end()){
                mmp[p1]->children.push_back(mmp[p2]);
            }else{
                mmp[p2]=new Node(n2,v2);
                mmp[p1]->children.push_back(mmp[p2]);
            }
        }
    }
    if(!isValid(mmp[pRoot])){
        cout<<"-1"<<endl;
    }else{
        int version=-1;
        getVersion(root,lookup,version);
        cout<<version<<endl;
    }


    return 0;
}

发表于 2019-09-09 22:04:23 回复(0)

问题信息

上传者:小小
难度:
2条回答 2978浏览

热门推荐

通过挑战的用户