/*HDU 2072 Word Count---string processing*/#include<cstdio>//C can be changed into stdio.h#include <cstring>//C can be changed into String.hConst intMAXN = -;intMain () {Char*head1, *head2; CharA[MAXN]; CharB[MAXN][MAXN]; intI, K, Len, Cnt1, Cnt2; while(Gets (a)) {//End of string "#" is encountered if('#'= = a[0]){ Break; } //working with contiguous spaces at the endLen =strlen (a); for(i = len-1; I >=0; --i) { if(' '==A[i]) {A[i]=0;//spaces at the end are replaced with null characters } Else Break; }//For (i)//working with contiguous spaces at the beginningLen =strlen (a); if(len = =0){//It's all spaces. Special Handlingprintf"0\n"); Continue; } for(i =0; i < Len; ++i) { if(' '==A[i]) {A[i]=0; } Else{ //encountered non-whitespaceHead1 = a + i;//record a new start Break; } }//For (i)Len=strlen (HEAD1); //fill a space at the end of the string to handle the last wordHead1[len] =' '; Head1[len+1] =0; Head2=Head1; Cnt1=0;//cnt1 Count Words (possibly duplicates) for(i =0; I <= Len; ++i) { if(' '==Head1[i]) {Head1[i]=0; //The current point is a space, then a dot is not a space, then a point is the new beginning of the word if(' '! = Head1[i +1]){ //record the last word and update the number of wordsstrcpy (B[cnt1], head2); ++Cnt1; Head2= Head1 + i +1;//update the new word starting point } } }//For (i)//working with the same number of wordsCnt2 =Cnt1; for(i =0; i < cnt1; ++i) { if(b[i][0] !='#'){ for(k = i +1; K < Cnt1; ++k) { if(b[k][0] !='#'&& strcmp (B[i], b[k]) = =0){ --Cnt2; b[k][0] ='#';//have judged whether repetition is no longer judged } }//For (k)b[i][0] ='#';//to this and b[i] the same word the first character has all been set to ' # '}//if}//For (i)printf"%d\n", Cnt2); }//While (gets) return 0;}
View Code
HDU 2072 Word Count---string processing