zoukankan      html  css  js  c++  java
  • 3R

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

    Input

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

    Output

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

    Sample Input

    you are my friend
    #

    Sample Output

    4

    // 不是统计没有重复的单词的总数
     1 #include<stdio.h>
     2 #include<string.h>
     3 int main()
     4 {
     5     char text[1000], word[100], *p, *q;
     6     int c, len, j,k, flag;
     7     while(gets(text), text[0]!='#')
     8     {
     9         len=strlen(text); 
    10         for(int i=0;i<len;i++)
    11             if(text[i]==' ')
    12                 text[i]='';
    13         c=0;
    14         for(p=text;p<len+text;p+=k+1)
    15         {
    16             k=strlen(p); strcpy(word,p); flag=1;
    17             for(q=p+k+1;q<len+text;q+=j+1)
    18             {
    19                 j=strlen(q);
    20                 if(strcmp(word,q)==0)
    21                 { flag=0; break; }
    22             }
    23             if(flag) c++;
    24         }
    25         printf("%d
    ", c);
    26     }
    27     return 0;
    28 }
    WA
    // 在文章中选定一个单词,在其后遍历. 若遇到相同的单词就删除
     1 #include<stdio.h>
     2 #include<string.h>
     3 
     4 void del_word_from_sentence(char *p, int len)
     5 {
     6     int l=strlen(p);
     7     for(int i=0;i<len-l;i++)
     8         p[i]=p[l+1+i];
     9 }
    10 
    11 int main()
    12 {
    13     char text[1000], word[100], *p, *q;
    14     int c, len, j,k, flag;
    15     while(gets(text), text[0]!='#')
    16     {
    17         len=strlen(text); 
    18         for(int i=0;i<len;i++)
    19             if(text[i]==' ')
    20                 text[i]='';
    21         c=0;
    22         for(p=text;p<len+text;p+=k+1)
    23         {
    24             k=strlen(p); strcpy(word,p); flag=0; c++;
    25             for(q=p+k+1;q<len+text;q+=j+1)
    26             {
    27                 j=strlen(q);
    28                 if(strcmp(word,q)==0)
    29                 { flag++; del_word_from_sentence(q,text+len-q); }
    30             }
    31             if(flag) len-=flag*(k+1);
    32         }
    33         printf("%d
    ", c);
    34     }
    35     return 0;
    36 }
    WA*2
    // 每删掉一个单词就马上修改文章长度. 还是WA。。。
     1 #include<stdio.h>
     2 #include<string.h>
     3 
     4 void del_word_from_sentence(char *p, int len)
     5 {
     6     int l=strlen(p);
     7     for(int i=0;i<len-l;i++)
     8         p[i]=p[l+1+i];
     9 }
    10 
    11 int main()
    12 {
    13     char text[100000], word[100000], *p, *q;
    14     int c, len, j,k, flag;
    15     while(gets(text), text[0]!='#')
    16     {
    17         len=strlen(text); 
    18         for(int i=0;i<len;i++)
    19             if(text[i]==' ')
    20                 text[i]='';
    21         c=0;
    22         for(p=text;p<len+text;p+=k+1)
    23         {
    24             k=strlen(p); strcpy(word,p); c++;
    25             for(q=p+k+1;q<len+text;q+=j+1)
    26             {
    27                 j=strlen(q); flag=0;
    28                 if(strcmp(word,q)==0)
    29                 { flag=1; del_word_from_sentence(q,text+len-q); }
    30                 if(flag) len-=k+1;
    31             }
    32             
    33         }
    34         printf("%d
    ", c);
    35     }
    36     return 0;
    37 }
    WA*3
    // 很大的数组不应该作为局部变量(有些OJ会限制局部数组的大小),做成全局变量不容易错
    // 没有考虑连续空格的情况如:aa(空格)(空格)bbb
    // 其他注意项见代码
     1 #include<stdio.h>
     2 #include<string.h>
     3 char text[100000], word[100000], *p, *q;
     4 
     5 void del_word_from_sentence(char *p, int len)
     6 {
     7     int l=strlen(p);
     8     for(int i=0;i<len-l;i++)
     9         p[i]=p[l+1+i];
    10 }
    11 
    12 int main()
    13 {
    14     int c, len, j,k, flag;
    15     while(gets(text), text[0]!='#')
    16     {
    17         len=strlen(text); 
    18         for(int i=0;i<len;i++)
    19             if(text[i]==' ')
    20                 text[i]='';
    21         c=0;
    22         for(p=text;p<len+text;p+=k+1)
    23         {
    24             k=strlen(p); 
    25             if(k==0) continue;
    26             strcpy(word,p); c++;
    27             for(q=p+k+1;q<len+text;q+=j+1)
    28             {
    29                 j=strlen(q);
    30                 if(j==0) continue;
    31                 flag=0;
    32                 if(strcmp(word,q)==0)
    33                 { flag=1; del_word_from_sentence(q,text+len-q); }
    34                 if(flag)
    35                 { len-=k+1; j=-1; }    // 删除一个单词后q应不变,为凑q+=0,令j=-1. 
    36             }
    37         }
    38         printf("%d
    ", c);
    39     }
    40     return 0;
    41 }
    AC*2
    // 详见代码
     1 #include<iostream>    // cin->stdin, cout->stdout, endl->os.put('
    ')&os.flush()
     2 #include<string>    // string, getline()->gets()
     3 #include<set>        // set
     4 #include<sstream>    // istringstream
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     string text, w;
    10     while(getline(cin,text), text!="#")
    11     {
    12         set<string> word;
    13         istringstream stream(text);
    14         while(stream>>w)            // Extends the container by inserting new elements, 
    15             word.insert(w);            // effectively increasing the container size by the number of elements inserted.
    16         cout<<word.size()<<endl;    // Returns the number of elements in the set container.
    17     }
    18     return 0;
    19 }
    AC
    
    
  • 相关阅读:
    关于:nth-children 的几点总结
    JQ常用知识点总结(笔记篇)————
    ajax的数据处理
    快捷小技巧
    javascript获取select,checkbox,radio的值
    面试题
    canvas基础
    python中的线程之semaphore信号量
    PHP中$_POST和$_GET的用法
    php中echo、print、print_r、var_dump、var_export区别
  • 原文地址:https://www.cnblogs.com/goldenretriever/p/10357062.html
Copyright © 2011-2022 走看看