题解 | #代理服务器#

代理服务器

https://www.nowcoder.com/practice/1284469ee94a4762848816a42281a9e0

每次从代理列表中选取能“走”的最远的,每次的终点+1为下一次的起点,直至服务器列表遍历完

#include<iostream>
#include<vector>

using namespace std;
int getCount(vector<string>proxy, vector<string> server) {
    int count = 0;
    int index = 0;//server起始位置
    int jump = 0; //使用一个代理最多能访问多少服务器
    for ( ; index < server.size(); index += jump) {
        jump = 0;
        for (auto item : proxy) {
            int j = index;
            while (item != server[j] && j < server.size()) {
                j++;

            }
            jump = max(jump, j - index);
        }
        if (jump == 0) return -1;
        count++;
    }

    return count - 1;
}
int  main() {
    int proxyNum;
    while (cin >> proxyNum) {
        int serverNum;
        vector<string> proxy;
        vector<string> server;
        for (int i = 0; i < proxyNum; i++) {
            string temp;
            cin >> temp;
            proxy.push_back(temp);
        }
        cin >> serverNum;
        for (int i = 0; i < serverNum; i++) {
            string temp;
            cin >> temp;
            server.push_back(temp);
        }
        int count = getCount(proxy,  server);
        cout << count << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

爱吃烤肠的牛油最喜欢...:50K是ssp了估计,ssp的人家多厉害都不用说,每年比例大概在百分之5左右
点赞 评论 收藏
分享
09-25 00:00
已编辑
电子科技大学 Java
球球与墩墩:这不是前端常考的对象扁平化吗,面试官像是前端出来的 const flattern = (obj) => { const res = {}; const dfs = (curr, path) => { if(typeof curr === 'object' && curr !== null) { const isArray = Array.isArray(curr); for(let key in curr) { const newPath = path ? isArray ? `${path}[${key}]` : `${path}.${key}` : key; dfs(curr[key], newPath); } } else { res[path] = curr } } dfs(obj); return res; }
查看3道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务