C + + Word Solitaire

Source: Internet
Author: User

Problem Description:

  1. RAM has just begun to learn English letters, the word sort is very interesting, he can quickly determine whether the words can be arranged in a list, so that the first letter of any word in the list with the last word of the same letter, force can write a computer program to help RAM to judge?
  2. The input to the function canarrangewords contains an integer num and a word arr. Num represents the number of words in the list (1<=num<=100), and arr should contain only words that consist of ' a '-' Z ' (word length <=100 in 2<=arr)
  3. If the words in the list are arranged as required, return 1; otherwise-1. In this arrangement, the first word in the list can start with any letter and does not need to satisfy the constraint.
  4. Test Case:
  5. testcase 1:input:4, [ abcd,defg,ghij,jkl] expected return value:1   
  6. TestCase 2:INPUT:4,[GHIJ,DEFG,JKL,ABCD] Expected Return value:-1

Problem Analysis:

  1. 1. Define two variables that represent the start and end of the current match
  2. 2. For the next word, it is possible to match the first and last words, or it may be the tail match, updating start and end respectively.
  3. 3. If the next word does not match the current, we put the word into the end of the container (because the match may be completed later)
  4. 4. In this loop, all matches are successful, or the total number of words matching and mismatched exceeds the total number of words
  5. 5. According to the above 1-4 steps, it is easy to associate the queue as the data structure.
    1 using namespacestd;2 intCanarrangewords (intNumChar**arr) {3 if(num<1|| Num> -)4 return-1;5queue<string>Qs;6  for(intI=0; i<num;i++)7 {8 Qs.push (Arr[i]);9 intlen=strlen (Arr[i]);Ten if(len<2|| Len> -) One return-1; A } - intCount=1; - intCountr=0; the CharHead=qs.front (). at (0); - CharEnd=qs.front (). at (strlen (arr[0]-1); - Qs.pop (); -  while(!qs.empty () && (count+countr<=num)) + { - intlen=strlen (Qs.front (). C_STR ()); + if(Qs.front (). at (0)==end) A { atEnd=qs.front (). at (len-1); - Qs.pop (); -count++; - } - Else if(Qs.front (). at (len-1)==head) - { inHead=qs.front (). at (0); - Qs.pop (); tocount++; + } - Else the { * Qs.push (Qs.front ()); $ Qs.pop ();Panax Notoginsengcountr++; - } the } + if(count==num) A return 1; the return-1; +}

C + + Word Solitaire

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.