“算法分析与设计”作业第三题,以前做过。
AC:
#include <iostream>
#include <map>
using namespace std;
class Array
{
public:
Array() {};
string& operator[]( const string s )
{
if ( !(indexSubscript.count(s)) )
indexSubscript.insert( make_pair( s, "7" ) );
iter = indexSubscript.find( s );
return (*iter).second;
}
public:
map<string, string> indexSubscript;
map<string, string>::iterator iter;
};
int main()
{
int cases;
cin >> cases;
while (cases)
{
Array list;
for (int i = 0; i < cases; i++)
{
string mj, address;
cin >> mj >> address;
int flag = 0;
for (map<string, string>::iterator it = list.indexSubscript.begin();
it != list.indexSubscript.end(); it++)
{
if ((*it).second == address)
{
(*it).second = mj;
flag = 1;
break;
}
}
if (flag == 0)
list.indexSubscript[mj] = address;
}
for (map<string, string>::iterator it = list.indexSubscript.begin();
it != list.indexSubscript.end(); it++)
{
cout << (*it).second << " is the MaJia of " << (*it).first << endl;
}
cout << endl;
cin >> cases;
}
return 0;
}