hdoj-2072-number of words (struct, Tai hang)

Source: Internet
Author: User

Number of wordsTime limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submission (s): 35874 Accepted Submission (s): 8664

problem DescriptionLily's good friend xiaoou333 recently, he thought of a meaningless thing, is to count the number of different words in an article. Your task below is to help xiaoou333 solve the problem.  
InputThere 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.  
Outputeach group only outputs an integer, which represents the total number of different words in a single story.  
Sample Input
You are my friend#
 
Sample Output
4
Problem Solving Ideas:At first, think of the need to store each of the different words, and then think of the structure of the array , the structure can have Word elements, but also the number of words elementsfind each word in turn to compare it with the words in the struct, if it is not the same, the struct array +1, otherwise the number of words in the struct +1string consists of lowercase letters and spaces, to consider the front is a space , followed by a space , There are spaces in the middle, more than one space 1, from the forward to determine whether it is a space, the string end of the space to exclude all2, starting from the front to determine whether it is a space, the space in front of each word to exclude all3. Temporarily store the word in an array (before storing the array to initialize each time, to avoid affecting the next operation)4, from the structure of the first array of arrays to determine whether the same as the temporary word, the same structure in the number of words +1, otherwise the word into the structure of the array, the number of structure array group +1. (Note that every time you look for a word or find the end of a space <len)5. Number of output structure array groupsExperimental Data  
1. "              "  .  Asdf as a "  ."  ASDF     asdf  ds "  24." ASDF   asdf    "  15. Direct Input Enter  0
6. "G   g"
  1
Code:
#include <stdio.h> #include <string.h>struct word{char nam[20];int num;} Words[1100];char s[1100];//Store string char z[1100];//temporary hold word int main () {int i,j,k;int len;int now;int sum;int ok;while (gets (s) &&s[0]!= ' # ') {Len=strlen (s); memset (words,0,sizeof (words)); for (i=len-1;i>=0;i--)    if (s[i]!= ") Break        ;    else        len--;//len=i; if there is a space behind it, if there is no space later then error     //exclude the space after the string i=0;sum=0;while (I<len) {for (; i<len;i++)    if (s[i]!= ')        break;//excludes the space memset (z,0,sizeof (z)) before the word, or//the word temporarily stores the array each time you want to empty the for (now=0;s[i]!= ' &&i< len;i++,now++)    z[now]=s[i];//at this time I have ==len ok=0;for (j=0;j<sum;j++)    if (strcmp (Z,words[j].nam) ==0)    {        words[j].num++;        ok=1;        break;    } if (ok==0) {strcpy (words[sum].nam,z); sum++;}        } printf ("%d\n", sum);} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdoj-2072-number of words (struct, Tai hang)

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.