-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalinwords_UVA257.cpp
83 lines (73 loc) · 1.83 KB
/
palinwords_UVA257.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
#include <bits/stdc++.h>
using namespace std;
ifstream fin("palinwords_UVA257.in");
ofstream fout("palinwords_UVA257.out");
bool isPalindrome(string & s)
{
int n = s.size();
for (int l = 0, r = n - 1; l <= r; ++l, --r)
{
if (s[l] != s[r]) return false;
}
return true;
}
bool isSame(string & a, string & b)
{
int sizeA = a.size(), sizeB = b.size();
if (sizeA < sizeB)
{
for (int i = 0; i + sizeA - 1 <= sizeB - 1; ++i)
{
if (a == b.substr(i, sizeA)) return true;
}
return false;
}
else
{
for (int i = 0; i + sizeB - 1 <= sizeA - 1; ++i)
{
if (b == a.substr(i, sizeB)) return true;
}
return false;
}
}
int main()
{
while (true)
{
string s = ""; fin >> s;
if (s == "") break;
int n = s.size();
set<string> p_s;
for (int i = 0; i <= n - 1; ++i)
{
for (int len = 3; i + len - 1 <= n - 1 && len <= 4; ++len)
{
string now = s.substr(i, len);
if (isPalindrome(now) == true) p_s.insert(now);
}
}
vector<string> p;
for (auto it = p_s.begin(); it != p_s.end(); ++it)
{
p.push_back(*it);
}
bool isPalinword = false;
int size = p.size();
for (int i = 0; i <= size - 1 && isPalinword == false; ++i)
{
for (int j = i + 1; j <= size - 1 && isPalinword == false; ++j)
{
if (isSame(p[i], p[j]) == false)
{
isPalinword = true;
}
}
}
if (isPalinword == true)
{
fout << s << '\n';
}
}
return 0;
}