3R - 单词数

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

Input

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

Output

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

Sample Input

you are my friend
#

Sample Output

4

// 不是统计没有重复的单词的总数
 #include<stdio.h>
#include<string.h>
int main()
{
char text[], word[], *p, *q;
int c, len, j,k, flag;
while(gets(text), text[]!='#')
{
len=strlen(text);
for(int i=;i<len;i++)
if(text[i]==' ')
text[i]='\0';
c=;
for(p=text;p<len+text;p+=k+)
{
k=strlen(p); strcpy(word,p); flag=;
for(q=p+k+;q<len+text;q+=j+)
{
j=strlen(q);
if(strcmp(word,q)==)
{ flag=; break; }
}
if(flag) c++;
}
printf("%d\n", c);
}
return ;
}

WA

// 在文章中选定一个单词,在其后遍历. 若遇到相同的单词就删除
 #include<stdio.h>
#include<string.h> void del_word_from_sentence(char *p, int len)
{
int l=strlen(p);
for(int i=;i<len-l;i++)
p[i]=p[l++i];
} int main()
{
char text[], word[], *p, *q;
int c, len, j,k, flag;
while(gets(text), text[]!='#')
{
len=strlen(text);
for(int i=;i<len;i++)
if(text[i]==' ')
text[i]='\0';
c=;
for(p=text;p<len+text;p+=k+)
{
k=strlen(p); strcpy(word,p); flag=; c++;
for(q=p+k+;q<len+text;q+=j+)
{
j=strlen(q);
if(strcmp(word,q)==)
{ flag++; del_word_from_sentence(q,text+len-q); }
}
if(flag) len-=flag*(k+);
}
printf("%d\n", c);
}
return ;
}

WA*2

// 每删掉一个单词就马上修改文章长度. 还是WA。。。
 #include<stdio.h>
#include<string.h> void del_word_from_sentence(char *p, int len)
{
int l=strlen(p);
for(int i=;i<len-l;i++)
p[i]=p[l++i];
} int main()
{
char text[], word[], *p, *q;
int c, len, j,k, flag;
while(gets(text), text[]!='#')
{
len=strlen(text);
for(int i=;i<len;i++)
if(text[i]==' ')
text[i]='\0';
c=;
for(p=text;p<len+text;p+=k+)
{
k=strlen(p); strcpy(word,p); c++;
for(q=p+k+;q<len+text;q+=j+)
{
j=strlen(q); flag=;
if(strcmp(word,q)==)
{ flag=; del_word_from_sentence(q,text+len-q); }
if(flag) len-=k+;
} }
printf("%d\n", c);
}
return ;
}

WA*3

// 很大的数组不应该作为局部变量(有些OJ会限制局部数组的大小),做成全局变量不容易错
// 没有考虑连续空格的情况如:aa(空格)(空格)bbb
// 其他注意项见代码
 #include<stdio.h>
#include<string.h>
char text[], word[], *p, *q; void del_word_from_sentence(char *p, int len)
{
int l=strlen(p);
for(int i=;i<len-l;i++)
p[i]=p[l++i];
} int main()
{
int c, len, j,k, flag;
while(gets(text), text[]!='#')
{
len=strlen(text);
for(int i=;i<len;i++)
if(text[i]==' ')
text[i]='\0';
c=;
for(p=text;p<len+text;p+=k+)
{
k=strlen(p);
if(k==) continue;
strcpy(word,p); c++;
for(q=p+k+;q<len+text;q+=j+)
{
j=strlen(q);
if(j==) continue;
flag=;
if(strcmp(word,q)==)
{ flag=; del_word_from_sentence(q,text+len-q); }
if(flag)
{ len-=k+; j=-; } // 删除一个单词后q应不变,为凑q+=0,令j=-1.
}
}
printf("%d\n", c);
}
return ;
}

AC*2

// 详见代码
 #include<iostream>    // cin->stdin, cout->stdout, endl->os.put('\n')&os.flush()
#include<string> // string, getline()->gets()
#include<set> // set
#include<sstream> // istringstream
using namespace std; int main()
{
string text, w;
while(getline(cin,text), text!="#")
{
set<string> word;
istringstream stream(text);
while(stream>>w) // Extends the container by inserting new elements,
word.insert(w); // effectively increasing the container size by the number of elements inserted.
cout<<word.size()<<endl; // Returns the number of elements in the set container.
}
return ;
}

AC