Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Object Oriented ProgrammingRSS Feeds

Program to help Umar in this task. Represent each city by one lowercase character. Thus, you can have at most 26 different cities to visit....

Posted By: Easy Tutor     Category: C++ Programming     Views: 1665

Umar is planning to take some rest and to go out on vacations, but he really doesn’t know which cities he should visit. So, he asked his parents for his help. His mother said “My son, you MUST visit Paris, Madrid, Lisbon and London. But it’s only fun in this order.” Then his father said: “Son, go to Paris, then to Lisbon, then to London and then, at last, go to Madrid.”

He decided that he’ll try to follow their suggestions in the best way he can. So he realized that the “Paris-Lisbon-Landon” order is the one which best satisfies both his mother and his father. If his father had suggested the “Landon-Paris-Lisbon-Madrid” then he would have two orders, “Paris-Lisbon” and “Paris-Madrid” that would better satisfy both of his parents. In this case, he could only visit 2 cities.

Your job is to write a program to help Umar in this task. Represent each city by one lowercase character. Thus, you can have at most 26 different cities to visit. But it’s possible that you’ll visit some city more than one.

The input will consist on a arbitrary number of city sequence pairs. The end of input occurs when the first sequence starts with a “#” character (without the quotes). Your program should not process this case. Each travel sequence will be on a line alone. All travel sequences will appear in a single line and will have at most 100 cities.

For each sequence pair, you should print the maximum number and name of the cities you can visit such that you’ll satisfy both the father’s and mother’s suggestions.

Code for Program to help Umar in this task. Represent each city by one lowercase character. Thus, you can have at most 26 different cities to visit.... in C++ Programming



 # include <iostream.h>
 # include <fstream.h>
 # include <string.h>
 # include <stdlib.h>
 # include <conio.h>

 int main( )
    {
       clrscr( );

       fstream File("CP-12.txt",ios::in|ios::nocreate);

       if(!File)
      {
         cout<<"\n Unable to open the input file."<<endl;
         cout<<"\n Press any key to exit.";

         getch( );
         exit(EXIT_FAILURE);
      }

       char Input_1[105]={NULL};
       char Input_2[105]={NULL};
       char Output_1[105]={NULL};
       char Output_2[105]={NULL};

       int maximum_cities_1=0;
       int maximum_cities_2=0;

       do
      {
         strset(Input_1,NULL);
         strset(Input_2,NULL);
         strset(Output_1,NULL);
         strset(Output_2,NULL);

         maximum_cities_1=0;
         maximum_cities_2=0;

         File.getline(Input_1,100);

         if(strcmp(Input_1,"#")==0)
        break;

         File.getline(Input_2,100);

         int i=0;
         int j=0;
         int k=0;
         int last_match=0;

         for(i=0;i<strlen(Input_1);i++)
        {
           for(j=last_match;j<=i;j++)
              {
             if(Input_1[i]==Input_2[j])
                {
                   Output_1[k]=Input_1[i];
                   last_match=j+1;
                   k++;
                   maximum_cities_1++;

                   break;
                }
              }
        }

         k=0;
         last_match=0;

         for(i=0;i<strlen(Input_2);i++)
        {
           for(j=last_match;j<=i;j++)
              {
             if(Input_2[i]==Input_1[j])
                {
                   Output_2[k]=Input_2[i];
                   last_match=j+1;
                   k++;
                   maximum_cities_2++;

                   break;
                }
              }
        }

         if(maximum_cities_1>=maximum_cities_2)
        cout<<maximum_cities_1<<"  "<<Output_1<<endl;

         else
        cout<<maximum_cities_2<<"  "<<Output_2<<endl;
      }
       while(1);

       getch( );
       return 0;
    }
  
Share: 



Easy Tutor
Easy Tutor author of Program to help Umar in this task. Represent each city by one lowercase character. Thus, you can have at most 26 different cities to visit.... is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!