题目思路:
首先读入所有的代理服务器,用map<string, bool>存储,代理地址到可否使用的映射关系。然后依次读入需要访问的服务器,读入一个地址后判断是否存在在代理服务中,如果存在,将该地址的代理使用权限标记为false,并判断是否全部的代理都标记false,如果全部都被标记false,那说明,这是离上次变更最远的了,必须进行变更代理服务器了,然后重置代理map,并标记当前的代理为false。
有个测试点需要输出-1,我就直接针对这个点进行了判断,可能还有更好的判断条件,期待回复。(PS:我的判断是代理数量为1,但需要访问它本身)
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
map<string, bool> agent;
void reset()
{
for (auto it = agent.begin(); it != agent.end(); it++)
(*it).second = true;
}
bool all_false()
{
for (auto it = agent.begin(); it != agent.end(); it++)
if ((*it).second == true)
return false;
return true;
}
int main()
{
int n, m;
while (cin >> n)
{
string tmp;
for (int i = 0; i < n; i++)
{
cin >> tmp;
agent[tmp] = true;
}
cin >> m;
int cnt = 0;
for (int i = 0; i < m; i++)
{
cin >> tmp;
if (agent.count(tmp) == 1)
{
agent[tmp] = false;
if (all_false() == true)
{
reset();
agent[tmp] = false;
cnt++;
}
}
}
if (agent.size() == 1 && cnt != 0)
cout << -1 << endl;
else
cout << cnt << endl;
}
} 
京公网安备 11010502036488号