-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96bff89
commit aeed3e9
Showing
7 changed files
with
968 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# Lab-01 | ||
In school days, we used to play a game called FLAMES (F: Friend, L: Love, A: Attraction, M: Marriage, E: Enemy, S: Sibling); which finds the relation between two names. Write a program that finds the relation between two names using the algorithm of FLAMES. | ||
|
||
[Disclaimer: Examples or names in this problem statement are unintentional. Any resemblance in the real world is a mere coincidence.] | ||
|
||
An example: Let’s check the relation between "abhishek" and "aishwarya". Remove the common characters from both the names. So, in the above problem the characters a, i, s and h are common. (Remember there are three a in second name. But only one a in first name and hence only once occurrence is common.) Now, the remaining number of characters in both the names is 4+5 = 9; which is the number of iterations required for eliminating a letter from FLAMES. | ||
|
||
Now, you have to check the number 9 with FLAMES as explained below. | ||
|
||
State 0: FLAMES | ||
|
||
Run/Count through FLAMES with number 9 like, | ||
|
||
F(1) L(2) A(3) M(4) E(5) S(6) F(7) L(8) A(9) M() E() S() | ||
|
||
So, 9 ends at A. Now delete A from FLAMES. Hence, remaining letters are FLMES | ||
|
||
State 1: FLMES | ||
|
||
Now, you have to start from M, i.e, the last position you were at. | ||
|
||
F() L() M(1) E(2) S(3) F(4) L(5) M(6) E(7) S(8) F(9) L() M() E() S() | ||
|
||
Delete F from FLMES. Hence, remaining letters are LMES. | ||
|
||
State 2: LMES - Starting point: L | ||
|
||
L(1) M(2) E(3) S(4) L(5) M(6) E(7) S(8) L(9) M() E() S() | ||
|
||
Delete L. Remaining MES. | ||
|
||
State 3: MES - Starting point: M | ||
|
||
M(1) E(2) S(3) M(4) E(5) S(6) M(7) E(8) S(9) M() E() S() | ||
|
||
Delete S. Remaining ME. | ||
|
||
State 4: ME - Starting point: M | ||
|
||
M(1) E(2) M(3) E(4) M(5) E(6) M(7) E(8) M(9) E() | ||
|
||
Delete M. Remaining E. | ||
|
||
State 5: E | ||
|
||
The remaining character E gives the relation between the two persons: (abhishek, aishwarya) : Enemies. | ||
|
||
Function Prototypes: | ||
|
||
// Function to shift characters of a string to left indices. | ||
void shiftLeft(string& str, int pos); | ||
Ex. If str contains "abcdefgh" and pos is 4. Then shiftLeft function deletes 'd' and modifies str to "abcefgh". | ||
|
||
// Function to find the common characters between two strings. | ||
int findSame(string str1, string str2); | ||
Ex. If str1 is ”abcdab” and str2 is ”abxymnda”, findSame function finds the common characters 'a', 'b', 'd' and 'a' So the function returns integer 4. | ||
|
||
// Function to remove characters from the string 'str' after repeated 'iter' iterations on str. | ||
void solveFlames(string& str, int iter); | ||
Ex. If str is "FLAMES" and iter is 5, the string remaining after removing characters is F. (FLAMES -> FLAMS -> FLAS -> FLA -> FA -> F) | ||
|
||
Input Format | ||
|
||
Format: Two strings (names of the two persons) separated by a space | ||
|
||
Constraints | ||
|
||
Note: 1. The input would be in lower case. 2. No two names will have all common characters. | ||
|
||
Test Cases Set A: | ||
|
||
None of the names will have repeating characters (e.g. RIA and RIAN) | ||
Number of iterations required to remove a character in FLAMES is 1. | ||
Test Cases Set B: | ||
|
||
None of the names will have repeating characters | ||
There are no constraints on number of iterations required to remove a character in FLAMES. | ||
Test Cases Set C: | ||
|
||
Names can have repeating characters (e.g. AISHWARYA and ABHISHEK) | ||
There are no constraints on number of iterations required to remove a character in FLAMES. | ||
Output Format | ||
|
||
Format: [Length of Name1, Length of Name 2, Number of common characters, Relationship]. | ||
|
||
Sample Input 0 | ||
|
||
riya | ||
riyan | ||
Sample Output 0 | ||
|
||
[4,5,4,S] | ||
Explanation 0 | ||
|
||
r,i,y,a are common in both the strings, so the strings remaining after removing common characters from "riya" and "riyan" are " " and "n" respectively. Therefore the number of iterations required to remove a character from "FLAMES" will be 0 + 1 = 1. | ||
|
||
After 1st iteration f is removed, and lames is left. | ||
|
||
After 2nd iteration l is removed, and ames is left. | ||
|
||
After 3rd iteration a is removed, and mes is left. | ||
|
||
After 4th iteration m is removed, and es is left. | ||
|
||
After 5th iteration e is removed, and s is left. Remaining string: S | ||
|
||
Since the length of "riya" is 4 and of "riyan" is 5, no. of common character is 4, hence the output is [4,5,4,S] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
#define mod 1000000007 | ||
#define pb push_back | ||
#define mp make_pair | ||
#define PI 3.14159265358979323 | ||
#define debug(x) cout<<"Case "<<x<<": " | ||
#define For(i,n) for(long long i=0;i<n;i++) | ||
#define Frabs(i,a,b) for(long long i = a; i < b; i++) | ||
#define Frabr(i,a,b) for(long long i = a; i >=b; i--) | ||
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); | ||
typedef long long int ll; | ||
typedef long double ld; | ||
typedef unsigned long long int ull; | ||
typedef vector <int> vi; | ||
typedef vector <ll> vll; | ||
typedef pair <int, int> pii; | ||
typedef pair <ll, ll> pll; | ||
typedef vector < pii > vpii; | ||
typedef vector < pll > vpll; | ||
typedef vector <string> vs; | ||
|
||
//Handle:cyber_rajat | ||
|
||
|
||
// Complete the findCommon function below. | ||
int findCommon(string str1, string str2) { | ||
int len1=str1.size(); | ||
int len2=str2.size(); | ||
int cnt=0; | ||
for(int i=0;i<len1;i++) | ||
{ | ||
for(int j=0;j<len2;j++) | ||
{ | ||
if(str1[i]==str2[j]) | ||
{ | ||
str2[j]='*'; | ||
cnt++; | ||
break; | ||
} | ||
} | ||
} | ||
return cnt; | ||
} | ||
|
||
// Complete the shiftLeft function below. | ||
void shiftLeft(string &str, int pos) { | ||
|
||
|
||
} | ||
|
||
// Complete the solveFlames function below. | ||
void solveFlames(string &str, int iter) { | ||
//int index=iter-1; | ||
int index=0; | ||
int len=iter-1; | ||
while(str.size()!=1){ | ||
int temp=str.size(); | ||
if(index==temp){ | ||
index=0; | ||
index=(index+len)%str.size(); | ||
} | ||
else{ | ||
index=(index+len)%str.size(); | ||
} | ||
str.erase(index,1); | ||
} | ||
|
||
} | ||
|
||
int main() | ||
{ | ||
sync; | ||
// Input the two names and store them as strings. | ||
string a,b; | ||
cin>>a>>b; | ||
// Create variable flames of type string and initialize it to "FLAMES". | ||
string k="FLAMES"; | ||
// Call function findCommon with both the inputs as parameters and store the result in a variable: commonCount. | ||
int common=findCommon(a,b); | ||
// Using commonCount, calculate the number of iterations required to remove a character from "FLAMES" and store the result in the variable 'iter' of type integer. | ||
int len1=a.size(); | ||
int len2=b.size(); | ||
int iter=len1+len2-2*common; | ||
solveFlames(k,iter); | ||
// Call the function solveFlames with string flames and integer iter as parameters. | ||
|
||
// Print the appropriate result. | ||
cout<<"["<<len1<<","<<len2<<","<<common<<","<<k<<"]"<<endl; | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
#define mod 1000000007 | ||
#define pb push_back | ||
#define mp make_pair | ||
#define PI 3.14159265358979323 | ||
#define debug(x) cout<<"Case "<<x<<": " | ||
#define For(i,n) for(long long i=0;i<n;i++) | ||
#define Frabs(i,a,b) for(long long i = a; i < b; i++) | ||
#define Frabr(i,a,b) for(long long i = a; i >=b; i--) | ||
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); | ||
typedef long long int ll; | ||
typedef long double ld; | ||
typedef unsigned long long int ull; | ||
typedef vector <int> vi; | ||
typedef vector <ll> vll; | ||
typedef pair <int, int> pii; | ||
typedef pair <ll, ll> pll; | ||
typedef vector < pii > vpii; | ||
typedef vector < pll > vpll; | ||
typedef vector <string> vs; | ||
|
||
//Handle:cyber_rajat | ||
|
||
class trainlist | ||
{ | ||
public: | ||
string name; | ||
int num; | ||
string source; | ||
string destination; | ||
int seats; | ||
}; | ||
|
||
int scan_trains(int n,vector<trainlist>& v) | ||
{ | ||
int a,d,i; | ||
string e,b,c; | ||
trainlist temp; | ||
for(i=0;i<n;i++){ | ||
cin>>temp.name>>temp.num>>temp.source>>temp.destination>>temp.seats; | ||
v.push_back(temp); | ||
} | ||
return i; | ||
} | ||
|
||
int print_trains(vector<trainlist>& v) | ||
{ | ||
vector<trainlist>::iterator it; | ||
int x=0; | ||
for(it=v.begin();it!=v.end();it++) | ||
{ | ||
x++; | ||
cout<<(*it).name<<" "<<(*it).num<<" "<<(*it).source<<" "<<(*it).destination<<" "<<"500 "<<(*it).seats<<" "<<endl; | ||
} | ||
return x; | ||
} | ||
|
||
|
||
void cancel_ticket(vector<trainlist>& v) | ||
{ | ||
int a,b,x=-1; | ||
cin>>a>>b; | ||
vector<trainlist>::iterator it; | ||
for(it=v.begin();it!=v.end();it++) | ||
{ | ||
if((*it).num==a) | ||
{ | ||
if(((*it).seats+b)<=500) | ||
{ | ||
x=(*it).seats+b; | ||
(*it).seats=x; | ||
break; | ||
} | ||
else | ||
{ | ||
x=0; | ||
break; | ||
} | ||
} | ||
} | ||
if(x>0) | ||
{ | ||
//vector<trainlist>::iterator iti; | ||
cout<<"Train number to make cancellation: "<<a<<endl; | ||
cout<<"Number of tickets to be cancelled: "<<b<<endl; | ||
cout<<"Tickets cancelled. Number of Tickets Available: "<<x<<endl; | ||
} | ||
else if(x==0) | ||
{ | ||
cout<<"Train number to make cancellation: "<<a<<endl; | ||
cout<<"Number of tickets to be cancelled: "<<b<<endl; | ||
cout<<"Not Enough bookings to cancel."<<endl; | ||
} | ||
else if(x==-1) | ||
{ | ||
cout<<"Train number to make cancellation: "<<a<<endl; | ||
cout<<"Number of tickets to be cancelled: "<<b<<endl; | ||
cout<<"Train Number not in records."<<endl; | ||
} | ||
} | ||
|
||
int main() { | ||
sync; | ||
int n, m ,k; | ||
cin >> n; | ||
|
||
// declare a vector named trainList to contain train records | ||
vector<trainlist> v; | ||
|
||
if( n > 0 ){ | ||
cout << "Testing reading train records." << endl; | ||
|
||
// call scan_trains with n and trainList as parameters. | ||
int value=scan_trains(n,v); | ||
cout << "Total number of train records: " << value << endl << endl; | ||
} | ||
|
||
cin >> m; | ||
if( m > 0 ){ | ||
cout << "Testing reading train records again." << endl; | ||
|
||
// call scan_trains again with m and trainList as parameters. | ||
int value=scan_trains(m,v); | ||
cout << "Total number of train records: " << value+n << endl << endl; | ||
} | ||
|
||
|
||
cout << "Testing printing train records."<< endl; | ||
|
||
// call print_trains with trainList as parameter. | ||
int value=print_trains(v); | ||
cout << "Total number of train records printed: " << value<< endl << endl; | ||
|
||
cin >> k; | ||
if( k > 0 ){ | ||
cout << "Testing cancelling tickets." << endl; | ||
//int sum=0; | ||
for(int i=0; i<k; i++){ | ||
// call cancel_ticket with trainList as paramete | ||
cancel_ticket(v); | ||
|
||
} | ||
|
||
cout <<endl; | ||
cout<< "Testing printing train records after updation."<< endl; | ||
|
||
// call print_trains again | ||
int value=print_trains(v); | ||
cout << "Total number of train records printed: " << value << endl; | ||
} | ||
return 0; | ||
} |
Oops, something went wrong.