forked from ajahuang/UVa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVa 11221 - Magic square palindromes.cpp
60 lines (56 loc) · 1.33 KB
/
UVa 11221 - Magic square palindromes.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
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std;
bool isMSPalindrom(const string &s, size_t K)
{
// s.size() must be a square number.
if (K * K != s.size())
return false;
if (K == 1)
return true;
for (size_t add = 1; add <= K; add *= K)
{
string::const_iterator iter(s.begin());
string::const_reverse_iterator r_iter(s.rbegin());
for (size_t addCount = 0; addCount < K; ++addCount)
{
if (*iter != *r_iter)
return false;
iter += add;
r_iter += add;
}
}
return true;
}
bool isIgnoredChar(char c)
{
if (c < 'a' || c > 'z')
return true;
return false;
}
int main()
{
string s;
getline(cin, s);
stringstream ss(s);
size_t T;
ss >> T;
size_t Case = 1;
while ( T-- )
{
getline(cin, s);
// Remove all whitespace and punctuation symbols.
s.erase(remove_if(s.begin(), s.end(), isIgnoredChar), s.end());
size_t K = static_cast<size_t>(sqrt(static_cast<double>(s.size()) + 0e-9));
cout << "Case #" << Case++ << ":" << endl;
if (isMSPalindrom(s, K))
cout << K << endl;
else
cout << "No magic :(" << endl;
}
return 0;
}