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]='\0';
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\n", 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]='\0';
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\n", 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]='\0';
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\n", c);
35     }
36     return 0;
37 }
WA*3
// 详见代码
 1 #include<iostream>    // cin->stdin, cout->stdout, endl->os.put('\n')&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

猜你喜欢

转载自www.cnblogs.com/goldenretriever/p/10357062.html
今日推荐