HDoj-2072-单词数

单词数

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 10000
char  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);
结果为:123456
2. 取指定长度的字符串。如在下例中,取最大长度为4字节的字符串。
sscanf("123456 ", "%4s", buf);
printf("%s\n", buf);
结果为:1234
3. 取到指定字符为止的字符串。如在下例中,取遇到空格为止字符串。
sscanf("123456 abcdedf", "%[^ ]", buf);
printf("%s\n", buf);
结果为:123456
4. 取仅包含指定字符集的字符串。如在下例中,取仅包含1到9和小写字母的字符串。
sscanf("123456abcdedfBCDEF", "%[1-9a-z]", buf);
printf("%s\n", buf);
结果为:123456abcdedf
当输入:
sscanf("123456abcdedfBCDEF","%[1-9A-Z]",buf);
printf("%s\n",buf);
结果为:123456
5. 取到指定字符集为止的字符串。如在下例中,取遇到大写字母为止的字符串。
sscanf("123456abcdedfBCDEF", "%[^A-Z]", buf);
printf("%s\n", buf);
结果为:123456abcdedf
6、给定一个字符串iios/12DDWDFF@122,获取 / 和 @ 之间的字符串,先将 "iios/"过滤掉,再将非'@'的一串内容送到buf中
sscanf("iios/12DDWDFF@122", "%*[^/]/%[^@]", buf);
printf("%s\n", buf);
结果为:12DDWDFF
7、给定一个字符串“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;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值