Description
Count the total number of different words in a story.
Input
There are multiple sets of data, each group of rows, each group is a small article. Each small article is composed of lowercase letters and spaces, no punctuation, and when you encounter #, the input ends.
Output
Each group only outputs an integer, which represents the total number of different words in a single story.
Sample InputYou are my friend#Sample Output4
The first time I write without seeing the problem (different words) led to WA
The idea is not a problem after clarity, with a two-dimensional array to record each word in a line of sentences, after each word to add
Every word in the end is compared to the words in front of it, and nothing is count++.
#include <iostream>#include<cstdio>#include<cstring>Charword[ -][ -];Chars[1000000];intMainintargcChar**argv) { while(Gets (s), s[0]!='#') { intt=0, l=0; intlen=strlen (s); intI=0; while(i<Len) { if(s[i]==' ') { while(s[i]==' ') I++; } Else { while(s[i]!=' '&&s[i]!=' /') Word[t][l++]=s[i++];//Save a word in a two-dimensional groupword[t][l]=' /';//don't forget to add to the end of the wordt++; L=0; } } /*next to filter*/ intCount=0; intflag=0; for(intj=0; j<t;j++) {flag=0; for(intk=0; k<j;k++) { if(strcmp (word[k],word[j]) = =0)//If the word is found to be the same as a previous word, the number of words is not countedflag=1; } if(flag==0) Count++; } printf ("%d\n", Count); } return 0;}
Zzuli OJ 1178 Word Count