HDU 2072 Word Count---string processing

Source: Internet
Author: User

/*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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.