zoukankan      html  css  js  c++  java
  • 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 ", buf); 结果为:123456 2. 取指定长度的字符串。如在下例中。取最大长度为4字节的字符串。 sscanf("123456 ", "%4s", buf); printf("%s ", buf); 结果为:1234 3. 取到指定字符为止的字符串。

    如在下例中,取遇到空格为止字符串。

    sscanf("123456 abcdedf", "%[^ ]", buf); printf("%s ", buf); 结果为:123456 4. 取仅包括指定字符集的字符串。如在下例中,取仅包括1到9和小写字母的字符串。

    sscanf("123456abcdedfBCDEF", "%[1-9a-z]", buf); printf("%s ", buf); 结果为:123456abcdedf 当输入: sscanf("123456abcdedfBCDEF","%[1-9A-Z]",buf); printf("%s ",buf); 结果为:123456 5. 取到指定字符集为止的字符串。如在下例中。取遇到大写字母为止的字符串。

    sscanf("123456abcdedfBCDEF", "%[^A-Z]", buf); printf("%s ", buf); 结果为:123456abcdedf 6、给定一个字符串iios/12DDWDFF@122,获取 / 和 @ 之间的字符串,先将 "iios/"过滤掉。再将非'@'的一串内容送到buf中 sscanf("iios/12DDWDFF@122", "%*[^/]/%[^@]", buf); printf("%s ", buf); 结果为:12DDWDFF 7、给定一个字符串“hello, world”,仅保留world。(注意:“,”之后有一空格) sscanf(“hello, world”, "%*s%s", buf); printf("%s ", 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++++++++++++++++++++
    ",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
    ", cnt);
            }
            return 0;
    }
    


    版权声明:本文博主原创文章,我们随意转载!好东西要大家一起分享嘛!

    哈哈哈!。

  • 相关阅读:
    加载图片出现403的问题
    js字符串首字母大写的不同写法
    vue中使用两个window.onresize问题解决
    vue备用
    vue注册全局组件
    Java中Timer的用法
    笔记本设置wifi热点
    UVA 11401 Triangle Counting
    数论专题---除法表达式之高精度运算,扩展欧几里得算法
    能被2、3、4、5、6、7、8、9整除数的特征
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4835742.html
Copyright © 2011-2022 走看看