-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongest_Palindromic_Concatination.cpp
91 lines (72 loc) · 1.74 KB
/
Longest_Palindromic_Concatination.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Mr Gnanesh is working with words. He has given a list of words.
// Each word in the list contains only two lowercase letters [a-z].
// He wants to create biggest word BW, by concatenating words from the list, which
// is a palindrome too. He is allowed to use each word from the list atmost once.
// Return the length of the biggest word can be formed using the list.
// If it is impossible to create such word, return 0.
// Input Format:
// -------------
// Space separated strings, words[], two letter words.
// Output Format:
// --------------
// Print an integer result.
// Sample Input-1:
// ---------------
// ab ba dd
// Sample Output-1:
// ----------------
// 6
// Explanation:
// ------------
// The biggest word is, ab,dd,ba => abddba, which is a palindrome.
// Sample Input-2:
// ---------------
// pq rs sr mk km pq
// Sample Output-2:
// ----------------
// 8
// Explanation:
// ------------
// The biggest word is, rs,sr,mk,km => rsmkkmsr or mkrssrkm..etc,
// which is a palindrome.
// Sample Input-3:
// ---------------
// aa bb cc
// Sample Output-3:
// ----------------
// 2
#include<bits/stdc++.h>
using namespace std;
int main(){
string s1;
getline(cin,s1);
stringstream x1(s1);
string t1="";
vector<string> v;
while(getline(x1,t1,' ')){
v.push_back(t1);
}
int res=0;
unordered_map<string,int> m;
for(string a:v){
string s=a;
reverse(s.begin(),s.end());
if(m[s]>0){
res=res+4;
m[s]--;
}
else{
m[a]++;
}
}
for(auto key:m){
string a=key.first;
string s=a;
reverse(s.begin(),s.end());
if(s==a){
res=res+2;
break;
}
}
cout<<res;
}