HDoj-2072-单词数

来源:互联网 发布:情侣装好看的淘宝店铺 编辑:程序博客网 时间:2024/05/17 18:16

单词数

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


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

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

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

Sample Input
you are my friend#
 

Sample Output
4
 总会有一种适合你。。。
//*************Set****************/#include<iostream>#include<sstream>#include<string>#include<set>using namespace std;int main(){string line,word;set <string> list;while(getline(cin,line)&&line!="#"){list.clear();istringstream stream(line);while(stream>>word){if(list.end()==list.find(word))list.insert(word);}cout<<list.size()<<endl;}return 0;} 

//************fopen文件*******************/#include<stdio.h>#include<string.h>#define N 10000char  article[N],tmp[101],word[N][101];int main(){    int len,pos,k,cnt;    freopen("hdu_2072_in.txt","r",stdin);    freopen("hdu_2072_out.txt","w",stdout);    while(gets(article)!=NULL)    {       if(article[0]=='#') break;       cnt=0;       len=strlen(article);       pos=0;       while(pos<len)       {           sscanf(article+pos,"%s",tmp);           for(k=0;k<cnt;k++)                  if( strcmp(word[k],tmp)==0) break;           if( k==cnt )  strcpy(word[cnt++],tmp);            pos += strlen(tmp) + 1;       }       printf("%d/n",cnt);    }return 0;}
//************SSSSSSScanf*******//HDOJ——2072单词数SSCANF用法:(继qsort,bsearch,strchr后发现的又一好使的函数)sscanf与scanf类似,都是用于输入的,只是后者以键盘(stdin)为输入源,前者以固定字符串为输入源。例子:  1. 常见用法。char buf[512] ;sscanf("123456 ", "%s", buf);//此处buf是数组名,它的意思是将123456以%s的形式存入buf中!printf("%s\n", buf);结果为:1234562. 取指定长度的字符串。如在下例中,取最大长度为4字节的字符串。sscanf("123456 ", "%4s", buf);printf("%s\n", buf);结果为:12343. 取到指定字符为止的字符串。如在下例中,取遇到空格为止字符串。sscanf("123456 abcdedf", "%[^ ]", buf);printf("%s\n", buf);结果为:1234564. 取仅包含指定字符集的字符串。如在下例中,取仅包含1到9和小写字母的字符串。sscanf("123456abcdedfBCDEF", "%[1-9a-z]", buf);printf("%s\n", buf);结果为:123456abcdedf当输入:sscanf("123456abcdedfBCDEF","%[1-9A-Z]",buf);printf("%s\n",buf);结果为:1234565. 取到指定字符集为止的字符串。如在下例中,取遇到大写字母为止的字符串。sscanf("123456abcdedfBCDEF", "%[^A-Z]", buf);printf("%s\n", buf);结果为:123456abcdedf6、给定一个字符串iios/12DDWDFF@122,获取 / 和 @ 之间的字符串,先将 "iios/"过滤掉,再将非'@'的一串内容送到buf中sscanf("iios/12DDWDFF@122", "%*[^/]/%[^@]", buf);printf("%s\n", buf);结果为:12DDWDFF7、给定一个字符串“hello, world”,仅保留world。(注意:“,”之后有一空格)sscanf(“hello, world”, "%*s%s", buf);printf("%s\n", buf);结果为:world%*s表示第一个匹配到的%s被过滤掉,即hello被过滤了如果没有空格则结果为NULL。
#include <stdio.h>#include <string.h>#include <math.h>char word[1000];char arr[100][100];  //arr用于存储以前出现过的单词int main(){        int len, pos;        char temp[100];        while(gets(word) && strcmp(word, "#") != 0)//题目要求不是文章结尾为,注意strcmp用比较用“#”        {                len = strlen(word);//总长度(包括空格)                pos = 0;                int cnt = 0;                // pos加单词长度一直到>len                while(pos <= len)                {                        sscanf(word + pos, "%s", temp); //把一个单词存入temp,空格省去(sscanf应该是读到空格截止,对于sscanf,空格很重要)                        //word + pos为指针指向位置                        //printf("%s++++++++++++++++++++\n",temp);                        int k;                        for(k = 0; k < cnt; ++k)                                if(strcmp(temp, arr[k]) == 0)   //如果和以前存入的单词相同,则不计数                                    break;                        if(k == cnt)                                strcpy(arr[cnt++], temp);   //把temp存入arr,并计数器cnt加一                        pos += strlen(temp) + 1;// +1表示加上空格,最后一次pos肯定会大于len                }                printf("%d\n", cnt);        }        return 0;}


0 0
原创粉丝点击