华为机试、面试题目

1.求区间最高分

int getMax(int *pScore, int low, int high)
{
    if(low > high)
    {
        high = low ^ high;
        low = low ^ high;
        high = low ^ high;
    }
    int maxScore = pScore[low];
    for(int i = low + 1; i <= high; ++i)
    {
        if(maxScore < pScore[i])
        {
            maxScore = pScore[i];
        }
    }
 
    return maxScore;
}
 
int main()
{
    int N = 0;
    int M = 0;
    while(cin >> N >> M)
    {
        int *pScore = new int[N + 1];
        for(int i = 1; i <= N; ++i)
        {
            cin >> pScore[i];
        }
 
        for(int i = 0; i < M; ++i)
        {
            char ch = '\0';
            int low = 0;
            int high = 0;
 
            cin >> ch >> low >> high;
 
            if('Q' == ch)
            {
                cout << getMax(pScore, low, high) << endl;
            }
            else if('U' == ch)
            {
                pScore[low] = high;
            }
            else
            {
                // continue;
            }
        }
 
        delete[] pScore;
    }
 
    return 0;
}

 

2.开发一个简单错误记录功能小模块,能够记录出错的代码坐在的文件名称和行号。 

struct info {//记录出现的顺序,和次数
    int rank;
    int count;
    info(int rank, int count) {
        this->rank = rank;
        this->count = count;
    }
};
struct fullinfo {//一条完整的结果,字符串和次数
    string file;
    int rank;
    int count;
    fullinfo(string file, int rank, int count) {
        this->file = file;
        this->rank = rank;
        this->count = count;
    }
};
struct classcomp {//set的比较器
    bool operator()(const struct fullinfo& f1, const struct fullinfo& f2) {
        if (f1.count == f2.count)
            return f1.rank<f2.rank;
        return f1.count>f2.count;
    }
};


typedef struct info INFO;
typedef struct fullinfo FULLINFO;
int main() {
    unordered_map<string, INFO> record;
    unordered_map<string, INFO>::iterator it;
    unordered_map<string, INFO>::const_iterator itfind;
    set<FULLINFO, classcomp> ret;
    set<FULLINFO, classcomp>::iterator sit;
    string linestr;//一行输入
    string file;//文件名+行号
    int pos;//空格的位置
    int i = 1;
    while (getline(cin, linestr)) {
        if (linestr.length() == 0)
            break;
        pos = linestr.rfind("\\");
        file = linestr.substr(pos + 1);//拆分得到最后的filename和count
        itfind = record.find(file);//在map中查看是否已经有了该字符串,没有则插入,有则次数加1
        if (itfind == record.end()) {
            INFO tmpi(i, 1);
            record.insert(pair<string, INFO>(file, tmpi));
        }
        else {
            INFO tmpi(itfind->second.rank, itfind->second.count + 1);
            record.erase(file);
            record.insert(pair<string, INFO>(file, tmpi));
        }
        i++;
    }
    for (it = record.begin();it != record.end();it++) {
        FULLINFO tmpfull(it->first, it->second.rank, it->second.count);//构建排序的set集合
        ret.insert(tmpfull);
    }
    for (i = 0, sit = ret.begin();sit != ret.end() && i<8;++sit, ++i) {//最多输出8条记录,file少于16位
        if (file.find(" ") <= 16) {
            cout << (*sit).file << " " << (*sit).count << endl;
        }
        else {
            cout << (*sit).file.substr(file.find(" ") - 16) << " " << (*sit).count << endl;
        }

    }
    return 0;
}

 

3.求多个字符串的最长公共子字符串

//求字符串的子字符串
vector<string> SubString(string& str)
{
    int i, width, len = str.length();
    vector<string> res;
    if (len <= 0) return res;
    else if (len == 1)
    {
        res.push_back(str); return res;
    }

    width = len;
    while (width>=1)
    {
        for (i = 0; i + width <= len; i++)
        {
            res.push_back(str.substr(i, width));
        }
        --width;
    }
    return res;
}

string FindSubString(vector<string>& str)
{
    string res = "no substring";
    size_t len = str.size();
    if (len <= 1) return res;

    bool sign = false;
    auto var = SubString(str[0]);
    for (size_t i = 0; i < var.size(); i++)
    {
        for (size_t j = 0; j < len; j++)
        {
            if (str[j].find(var[i]) == str[j].npos)
            {
                sign = false; break;
            }
            else sign = true;
        }
        if (true == sign)
        {
            res = var[i]; break;
        }
    }
    return res;
}
posted @ 2015-09-09 10:14  从此寂静无声  阅读(642)  评论(0编辑  收藏  举报