Prepare for CET6

Prepare for CET6

时间: 1ms        内存:128M

描述:

Hard to force the CET4&6 is imminent, which makes most of the students a headache. This does not, Liu is trying his best to prepare for the CET6, trying to do a variety of previous year’s problem. But the most helpless for Liu is a quick read of this topic, so he thought of a thing does not make sense is that the statistical article the total number in different words. Here your task is to help Liu solve this problem.

输入:

Multiple sets of data, each row is an article. Each article is consisted by lowercase letters and spaces, no punctuation, encounter # when the input end.

输出:

Each group output only an integer, which alone make the trip, the integer representing the total number of different words in an article.

示例输入:

you are my friend
can you can a can as a canner can can a can
#

示例输出:

4
5

提示:

参考答案(内存最优[1488]):

#include <stdarg.h>
#include <iostream>
#include <math.h>
#include <iomanip>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int main(){
    string s;char t;
    while(getline(cin,s)&&s!="#"){
        string p[1000];
        int len=s.length(),i=0,count=0,z;
       while(i<len){
           while(s[i]!=' '&&i<len){
                t=s[i++];
                p[count]+=t;
           }
              ++count;
               i++;
       }
        sort(p,p+count);
        int oo=count,k=0;
        for(i=1;i<count;++i){
            k=0;
            while(p[i]==p[i-1]){
             k++;i++;
            }
        oo-=k;
        }
        cout<<oo<<'\12';
    }
    return 0;
}

参考答案(时间最优[0]):

#include<iostream>
#include<set>
#include<string>
#include<sstream>
using namespace std;
int main()
{
    string art;
    while(getline(cin,art) && art != "#")
    {
        istringstream stream(art);
        string word;
        set<string> map;
        while(stream >>word)
        {
            map.insert(word);
        }
        cout <<map.size() <<endl;
    }
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注