单词个数(不同单词数目) HDU-2072

#include<cstdio>
#include<iostream>
#include<cstring>
#define maxn 205
using namespace std;
char s[maxn][maxn],t[maxn],c;//s[maxn][maxn]用来存放不同的单词 ,统计个数。t用来存放每次刚输入的单词 
int main()
{
    int num,pre;
    while(scanf("%s%c",s[0],&c)!=EOF)
    {
       num = 1,pre = 1;
       while(c != '\n')
       {
          scanf("%s%c",t,&c);
             bool same = false;
             for(int i = 0;i<num;i++) //与已经存放的不同的单词比较,看是否相同
             {
                 if(strcmp(s[i],t)==0)
                   same = true;
          }
          if(!same)//不同,存放进s[maxn][maxn]
          {
            strcpy(s[num++],t);
          }
          pre++;
           
       }
       cout<<num<<" "<<pre<<endl;    
    }
    return 0;
} 





输入单词时,采用scanf("%s",s[num++]),可以很方便的输入单词。当遇见非空格时说明读入一个单词,遇见空格时说明单词读入结束。
字母c可以标记字符串是否读入结束,当c是'\n'时说明字符串读入结束,否则则没有。

lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。

Input有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。

Output每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。

Sample Input

you are my friend
#
Sample Output
4
//
//  main.cpp
//  练习
//
//  Created by Mallow on 2018/4/19.
//  Copyright © 2018年 Mallow. All rights reserved.
//
#include<cstdio>
#include<cstring>
#include<string>
#include<string.h>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#define ll long long
using namespace std;

int main(){
    std::ios::sync_with_stdio(false);
    set<string> ac;
    string m,s;
    while(getline(cin,m)){
        if(m=="#") break;
        s="";
        for(int i=0;i<m.length();i++){
            if(m[i]==' '||i==m.length()-1){
                if(s.length()>0){
                    ac.insert(s);
                }
                s="";
                continue;
            }
            s+=m[i];
        }
        cout<<ac.size()<<endl;
        ac.clear();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/MallowFlower/article/details/80181708
今日推荐