Danny has a possible list of passwords of Manny’s facebook account. All passwords length is odd. But Danny knows that Manny is a big fan of palindromes. So, his password and reverse of his password both should be in the list.
You have to print the length of Manny’s password and it’s middle character.
Note : The solution will be unique.
INPUT
The first line of input contains the integer N, the number of possible passwords.
Each of the following N lines contains a single word, its length being an odd number greater than 2 and lesser than 14. All characters are lowercase letters of the English alphabet.
OUTPUT
The first and only line of output must contain the length of the correct password and its central letter.
CONSTRAINTS
1 < = N <= 100
Sample Input
4
abc
def
feg
cba
Sample Output
3 b
Logic :
Store each password and its reverse in hash function.
for each password search weather its reverse is existing in hash or not.
If yes, return length+middle element of it.
else continue.
#include <stdio.h>
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
#define MAX 100
string reverse(string temp){
//cout<<" In function "<<temp;
char pwd[temp.length()];
int j=0;
for(int i=(temp.length())-1;i>=0;i--,j++){
pwd[j]=temp[i];
}
pwd[j]='\0';
string str(pwd);
return str;
}
int findPassword(string password[],int k){
string temp;
char *op;
unordered_map<string, string> ht;
for(int i=0;i<k;i++){
if(password[i].length()%2 == 0 ) {cout<<"error in length" ; return 0 ;}
temp=password[i];
ht.insert(make_pair(temp,reverse(temp)));
if(ht.find(reverse(temp))!=ht.end()){
//cout<<temp<< "<=>" <<reverse(temp)<<endl ;
cout<<password[i].length()<<" "<<temp[(password[i].length())/2];
return 1;
}//if
}//for
return 0;
}
int main(){
string password[MAX];
unordered_map<string, string> ht;
int i=0, j=0, k=0, t=0;
cin>>t;
k=t;
while(t){
cin>>password[i];
--t; ++i;
}
findPassword(password,k);
}//main
see working code here : http://ideone.com/WqbO5v
You have to print the length of Manny’s password and it’s middle character.
Note : The solution will be unique.
INPUT
The first line of input contains the integer N, the number of possible passwords.
Each of the following N lines contains a single word, its length being an odd number greater than 2 and lesser than 14. All characters are lowercase letters of the English alphabet.
OUTPUT
The first and only line of output must contain the length of the correct password and its central letter.
CONSTRAINTS
1 < = N <= 100
Sample Input
4
abc
def
feg
cba
Sample Output
3 b
Logic :
Store each password and its reverse in hash function.
for each password search weather its reverse is existing in hash or not.
If yes, return length+middle element of it.
else continue.
#include <stdio.h>
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
#define MAX 100
string reverse(string temp){
//cout<<" In function "<<temp;
char pwd[temp.length()];
int j=0;
for(int i=(temp.length())-1;i>=0;i--,j++){
pwd[j]=temp[i];
}
pwd[j]='\0';
string str(pwd);
return str;
}
int findPassword(string password[],int k){
string temp;
char *op;
unordered_map<string, string> ht;
for(int i=0;i<k;i++){
if(password[i].length()%2 == 0 ) {cout<<"error in length" ; return 0 ;}
temp=password[i];
ht.insert(make_pair(temp,reverse(temp)));
if(ht.find(reverse(temp))!=ht.end()){
//cout<<temp<< "<=>" <<reverse(temp)<<endl ;
cout<<password[i].length()<<" "<<temp[(password[i].length())/2];
return 1;
}//if
}//for
return 0;
}
int main(){
string password[MAX];
unordered_map<string, string> ht;
int i=0, j=0, k=0, t=0;
cin>>t;
k=t;
while(t){
cin>>password[i];
--t; ++i;
}
findPassword(password,k);
}//main
see working code here : http://ideone.com/WqbO5v