华为机试第二题

感觉挺简单的,但是一直卡在50没AC不知道什么原因,我考虑了最后一个TLV舍去问题了呀
大佬帮帮看看,一直困扰着不舒服
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>

using namespace std;
int hexToDec(const string& str);
int main()
{
    //输入
    string strTLV;
    int nTag;
    cin>>strTLV;
    cin>>nTag;
    vector<int> tag(nTag);
    for (int i=0; i<nTag; i++){
        cin >> tag[i];
    }

    //结束输入
    unordered_map<int, pair<int, int>> ump;
    int i=0;
    bool goodTLV=true;
    while (i<strTLV.size()-1&&goodTLV){
        if (i+4>strTLV.size()) break;
        int curTag=hexToDec(strTLV.substr(i,2));
        int length=hexToDec(strTLV.substr(i+2,2));
        //检测是否是完整的TLV
        if (i+length*2+4>strTLV.size()){
            goodTLV=false;
            break;
        }
        //如果是,加入数据
        ump[curTag].first=length;
        ump[curTag].second=i+2;
        i=i+length*2+4;
    }
    for (int i=0; i<nTag; i++){
        if (ump.find(tag[i])!=ump.end()){
            cout << ump[tag[i]].first << ' ' << ump[tag[i]].second << endl;
        }
        else {
            cout << 0 << ' ' << 0 << endl;
        }
    }

    return 0;
}
int hexToDec(const string& str)
{
    if (str.size()!=2) return -1;
    int ret=0;
    if (str[1]>='A') ret+=str[1]-'A'+10;
    else ret+=str[1]-'0';
    if (str[0]>='A') ret+=(str[0]-'A'+10)*16;
    else ret+=(str[0]-'0')*16;
    return ret;
}


#笔试题目##华为#
全部评论
思路一样,甚至代码也差不多,只过了45%
3
送花
回复
分享
发布于 2022-03-23 21:59
valueOffset是字节数,不是i+2,应该是i/2+2吧(我的思路,具体代码实现可能有点差别~
3
送花
回复
分享
发布于 2022-03-24 09:59
秋招专场
校招火热招聘中
官网直投
200*50%=100分 不就过了吗😂 是这么算不?
1
送花
回复
分享
发布于 2022-03-24 00:19
题目是啥呀
点赞
送花
回复
分享
发布于 2022-03-23 22:07
TLV解析:https://blog.nowcoder.net/n/b61697b48bdb41d4b343af523763471d
点赞
送花
回复
分享
发布于 2022-03-30 00:06

相关推荐

点赞 18 评论
分享
牛客网
牛客企业服务