HDU2072 number of words

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 79794    Accepted Submission(s): 20257

Problem Description

lily has been a very good friend xiaoou333 empty, he thought something did not make sense of things, is an article of statistics the total number of different words. Below you xiaoou333 mission is to help solve this problem.

Input

Multiple sets of data, each line, each is a small article. Each small articles are lowercase letters and spaces, no punctuation, # indicates the end of input is encountered.

Output

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

Sample Input

you are my friend #

Sample Output

4

 

code show as below:

A solution (AC):

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <set>
using namespace std;
int main(){
    char chr[1024];
    char stop[]="#";
    char prim[]=" ";
    char *p;
    set<string> words;
    while(gets(chr)!=NULL){
        if(strcmp(chr,stop)==0) break;
        words.clear();
        p=strtok(chr,prim);
        while(p){
            words.insert(p);
            p=strtok(NULL,prim);
        }
        cout<<words.size()<<endl;
    }
    return 0;
}
Solution two (AC):
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
using namespace std;
int main(){
    string str;
    set<string> words;
    while(getline(cin,str)&&(str!="#")){
        words.clear();
       istringstream sin(str);
       string s;
       while(sin>>s)
             words.insert(s);
       cout<<words.size()<<endl; 
    }
    return 0;
}
 

Guess you like

Origin www.cnblogs.com/jianqiao123/p/11373588.html