Sunday, December 10, 2017

Four Strings Rectangle - 2 Diff length

Four strings out of which two have the same length L1 and the remaining two have the same length L2 are passed as the input to the program. The four strings must be printed in a L1*L2 rectangular matrix shape as shown in the example input/output. L1 >= L2 and a string with L1 must appear on the top of the rectangle. The string which is on the top with length L1 will always be the first string in the input. Other three strings can occur in a random order in the input. The sequence of the string can be identified by the fact that the last letter of a string will be the first letter of another string (and you can safely assume the last letter will not occur more than once).

Input Format:
The first line contains the string which represents the top of the rectangular matrix. The next three lines will contain the remaining the three string values which can represent the right, left and bottom side of the rectangle, but not necessarily in the same order.

Output Format:
The L1*L2 rectangular matrix with these four strings as it's sides as described in the Example Input/Output.

Boundary Conditions:
 3 <= L1, L2 <= 100 L1 >= L2

Example Input/Output 1:
Input:
MANAGE
SUM
TAURUS
EAT
Output:
MANAGE
U****A
SURUAT

Solution-1:
#include <bits/stdc++.h>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
    string s[4];
    cin>>s[0]>>s[1]>>s[2]>>s[3];
    string l,r,m;
    for(int i=1;i<4;i++)
    {
        if(s[0][0]==s[i][s[i].length()-1])
        {
        l=s[i];
        s[i]="*";
        }
        else if(s[0][s[0].length()-1]==s[i][0])
        {
            r=s[i];
            s[i]="*";
        }
    }
    for(int i=1;i<4;i++)
    {
        if(s[i]!="*")
        m=s[i];
    }
    cout<<s[0]<<endl;
    int x=l.length()-2,y=1;
    for(int i=1;i<l.length()-1;i++)
    {
        for(int j=0;j<s[0].length();j++)
        {
            if(j==0)
            {
                cout<<l[x];x--;
            }
            else if(j==s[0].length()-1)
            {
                cout<<r[y];y++;
            }
            else
            cout<<"*";
        }
        cout<<endl;
    }
    reverse(m.begin(),m.end());
    cout<<m;
}
Share: