Problem Description
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
Sample Input
you are my friend #
Sample Output
4
题意:统计不同单词的个数
思路:找个数组记录出现过的单词,扫描每一个新的单词,判断之前是否出现过,然后总数加一或不变。
AC代码(stringstream):
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <sstream>
using namespace std;
int main()
{
string s, S[1000];
while(1)
{
getline(cin, s);
if(s == "#") break;
stringstream ss(s);
string t;
int tot = 0;
while(ss >> t)
{
int i;
for(i = 0; i < tot; ++i)
if(S[i] == t) break;
if(i == tot) S[tot++] = t;
}
cout << tot << endl;
}
return 0;
}
AC代码(map):
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
char str[10005];
int main()
{
int flag = 0;
while(gets(str))
{
if(str[0] == '#') break;
string s;
map <string,int> a;
int len = strlen(str);
for(int i = 0; i < len; ++i)
{
while(str[i] >= 'A' && str[i] <= 'z')
{
s += str[i];
i++;
flag = 1;
}
if(flag) //找到一个单词
{
a[s] = 1;
flag = 0;
s.clear();
}
}
cout << a.size() << endl;
}
return 0;
}
寒假共集训10场,年前年后的最后一场都是很基础的题。所以目前也只有这两场补完了。看参赛的队伍在慢慢减少,热情都有所减少吧..
要开学了,新的学期重拾当初的热情(⊙▽⊙) fighting~