华为机试第二题
感觉挺简单的,但是一直卡在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;
}
查看10道真题和解析