每次从代理列表中选取能“走”的最远的,每次的终点+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")