UVA 10815 Andy's First Dictionary (C++ STL map && set )

时间:2024-04-20 18:02:21

原题链接:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1756

这道题用STL解答还是比较简单的,下面就给大家分享用 set 和 map 的 AC 。

解法一(map):

#include <iostream>
#include <string>
#include <map> using namespace std;
map<string, char> mmp; int main(void)
{
int i, idex = 0;
char ch, str[200]; while (ch = getchar()) {
if (ch == EOF) break;
if (isupper(ch)) ch = tolower(ch);
if (islower(ch)) str[idex++] = ch;
else {
str[idex++] = '\0';
if (idex > 1) mmp[str] = 1;
idex = 0;
}
} for (map<string, char>::iterator i = mmp.begin(); i != mmp.end(); i++)
puts(i -> first.c_str()); return 0;
}

解法二(set):

#include <iostream>
#include <sstream>
#include <string>
#include <set> using namespace std;
set<string> se;
string x, y; int main(void)
{
while (cin >> x) {
for (int i = 0; i < x.length(); i++) {
if (isalpha(x[i]))
x[i] = tolower(x[i]);
else x[i] = ' ';
} stringstream ss(x);//文件流
while (ss >> y) se.insert(y);
} for (set<string>::iterator it = se.begin(); it != se.end(); it++)
cout << *it << '\n'; return 0;
}