-
Notifications
You must be signed in to change notification settings - Fork 0
/
#167.cpp
42 lines (36 loc) · 866 Bytes
/
#167.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
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void print_result(vector<pair<int, int>>& v) {
cout << "[ ";
for (pair<int, int> p : v) {
cout << "(" << p.first << ", " << p.second << ") ";
}
cout << "]" << endl;
}
bool is_palindrome(string s) {
for (int i = 0; i < s.length()/2; i++) {
if (s[i] != s[s.length()-1-i]) return false;
}
return true;
}
vector<pair<int, int>> find_concat_palindromes(vector<string>& v) {
vector<pair<int, int>> result;
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v.size(); j++) {
if (j != i) {
string s = v[i] + v[j];
if (is_palindrome(s)) {
result.push_back(make_pair(i, j));
}
}
}
}
return result;
}
int main() {
vector<string> v = {"code", "edoc", "da", "d"};
vector<pair<int, int>> result = find_concat_palindromes(v);
print_result(result);
}