zoukankan      html  css  js  c++  java
  • HDU 2072 单词数

    单词数

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


    Problem Description
    lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
     
    Input
    有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
     
    Output
    每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
     
    Sample Input
    you are my friend #
     
    Sample Output
    4
     
     
     
     
     
     
     
    这次我第一次对每一个字符处理。
    代码如此:
    View Code
     1 #include<stdio.h>
    2 #include<string.h>
    3 int main()
    4 {
    5 char c,str[1000][1000],flag = 0,count;
    6 int x,y,i,j;
    7
    8 while(1)
    9 {
    10 x = 0;
    11 y = 0;
    12 while((c = getchar() )!= '\n')
    13 {
    14 if (c == '#')
    15 {
    16 flag = 1;
    17 break;
    18 }
    19 if (c != ' ')
    20 {
    21 str[x][y++] = c;
    22 }
    23 else
    24 {
    25 str[x][y] = '\0';
    26 y = 0;
    27 x++;
    28 }
    29 }
    30
    31 if(flag)
    32 {
    33 break;
    34 }
    35 str[x][y] = '\0';
    36 count = 0;
    37 for (i = 0;i <= x;i++)
    38 {
    39 if(str[i][0])
    40 {
    41 count++;
    42 for(j = i + 1;j <= x;j++)
    43 {
    44 if(strcmp(str[i],str[j]) == 0)
    45 {
    46 str[j][0] = '\0';
    47 }
    48 }
    49 }
    50 }
    51 printf("%d\n",count);
    52 }
    53
    54 return 0;
    55 }


     1 #include<stdio.h>
    2 #include<string.h>
    3 int main()
    4 {
    5     char c,str[1000][1000],flag = 0,count;
    6     int x,y,i,j;
    7
    8     while(1)
    9     {
    10         x = 0;
    11         y = 0;
    12         while((c = getchar() )!= '\n')
    13         {
    14             if (c == '#')
    15             {
    16                 flag = 1;
    17                 break;
    18             }
    19             if (c != ' ')
    20             {
    21                 str[x][y++] = c;
    22             }
    23             else
    24             {
    25                 str[x][y] = '\0';
    26                 y = 0;
    27                 x++;
    28             }
    29         }
    30         
    31         if(flag)
    32         {
    33             break;
    34         }
    35         str[x][y] = '\0';
    36         count = 0;
    37         for (i = 0;i <= x;i++)
    38         {
    39             if(str[i][0])
    40             {
    41                 count++;
    42                 for(j = i + 1;j <= x;j++)
    43                 {
    44                     if(strcmp(str[i],str[j]) == 0)
    45                     {
    46                         str[j][0] = '\0';
    47                     }
    48                 }
    49             }
    50         }
    51         printf("%d\n",count);
    52     }
    53     
    54     return 0;
    55 }
     
    对STL不太了解,这里有个用STL做的代码。。先留下,以后慢慢学习。
    View Code
     1 #include<iostream>  
    2 #include<set>
    3 #include<string>
    4 #include<sstream>
    5 using namespace std;
    6 int main()
    7 {
    8 string art;
    9 while(getline(cin,art) && art != "#"){
    10 istringstream stream(art);
    11 string word;
    12 set<string> map;
    13 while(stream >>word){
    14 //cout <<word <<" ";
    15 map.insert(word);
    16 }
    17 cout <<map.size() <<endl;
    18 }
    19 return 0;
    20 }
     
     
    看了人家的报告后,学了点东西。1~#include<string.h>里的strtok()函数。这个我另外写一篇。介绍。(取由某字符串分隔的串)
    2~计算不同类的总数的方法。
    以前我是先排序,指向第一个元素,count = 1,向后指,当当前元素与前一个元素不同时count++.
     
     
    而人家是,用另外一个数组y[0....l],存不同类的元素,l  记录长度,开始l = 0;  对于每个元素都要把数组y[0...l]扫完。若发现相同就break;,扫完都没相同,新增一个不同的类,y[l] = 新的一类。l++;
    最后的 l 就是不同类的总数了。看代码。
    View Code
     1 #include<stdio.h> 
    2 #include<string.h>
    3
    4 char y[1000][100];
    5 char x[20001],t[100];
    6 int main()
    7 {
    8 int i,j,k,l;
    9 char *p,*q;
    10 while(gets(x))
    11 {
    12 if(strcmp(x,"#")==0) break;
    13 l=0;
    14 p=x;
    15 q=strtok(p," "); //第一次用是p,以后是NULL
    16 while(q)
    17 {
    18 strcpy(t,q);
    19 for(i=0;i<l;i++)//上面说的计算不同类的总数。
    20 {
    21 if(strcmp(y[i],t)==0)
    22 break;
    23 }
    24 if(i==l)
    25 {
    26 strcpy(y[i],t);
    27 l++;
    28 }
    29 q=strtok(NULL," ");
    30 }
    31
    32 printf("%d\n",l);
    33 }
    34 return 0;
    35 }
  • 相关阅读:
    实验17:NAT
    实验16:ACL
    实验15: STP
    实验14:VLAN间的路由
    实验13:VLAN/TRUNK/VTP/
    Linux软件管理--RPM工具
    Linux拓展练习部分--输入输出 / find部分 /基础拓展2
    linux文件管理--压缩打包
    find 文件查找
    防火墙知识点
  • 原文地址:https://www.cnblogs.com/qiufeihai/p/2323371.html
Copyright © 2011-2022 走看看