统计不同单词的个数

#pragma warning (disable:4786)
#include<cstdio>
#include<iostream>
#include<string>
#include<string.h>
#include<set>
using namespace std;
int main()
{
    set<string> st;
    string s="";
    char c;
    while((c=getchar())!='#')
    {
        s+=c;
        while(c!='\n')
        {
            while((c=getchar())!=' '&&c!='\n')
                s+=c;
            if(s.length())st.insert(s);
            s="";
        }
        cout<<st.size()<<endl;
        st.clear();
    }
    return 0;
}
 

 

#include<cstdio>
#include<iostream>
#include<string>
#include<string.h>
#include<map>
using namespace std;
char a[1000000];
int main()
{
    map<string,int>tree;
    string s;
    int i,len,j;
    while(gets(a))
    {

        if(a[0]=='#')break;
        len=strlen(a);
        tree.clear();
        for(i=0;i<len;i++)
        {
            if(a[i]=='#')
            break;
            if(a[i]!=' ')
            {
                s="";
                for(j=i;j<len;++j)
                {
                    if(a[j]==' ')
                    break;
                    s+=a[j];
                }
                tree[s]++;
            }
            i=j;
        }
        printf("%d\n",tree.size());
    }
    return 0;
}

 

posted @ 2012-11-17 18:40  myth_HG  阅读(317)  评论(0编辑  收藏  举报