因为新学了散列表容器map,这道题只用统计不同单词的总数,用映射再统计个数蛮合适,学以致用doge,需要注意文章开头可能有空格,最后要把空格这一映射减掉。 AC代码:#include <iostream>
#include <cstdio>
#include <map>
#include <string>
using namespace std;
map<string, int> myMap;
int main() {
string str;
while(getline(cin, str)) {
if(str == "#")
break;
int j = 0;
map<string, int>::iterator it;
for(int i = 0; i < str.size(); i = j+1) {
j = str.find(" ", i);
string str2;
if(j >= 0) {
str2 = str.substr(i, j-i);
} else {
str2 = str.substr(i);
j = str.size();
}
it = myMap.find(str2);
if(it == myMap.end()) {
myMap.insert(pair<string, int>(str2, 1));
}
}
int answer = myMap.size();
if(myMap[""] != 0) {
answer--;
}
cout << answer << endl;
myMap.clear();
}
return 0;
}