-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path98 - Anagramic squares.cpp
135 lines (123 loc) · 2.84 KB
/
98 - Anagramic squares.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <vector>
#include <map>
#include <unordered_set>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned ll
#define db double
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PII pair<int, int>
char buf[20];
vector<string> vec;
vector<ll> mt[2000];
vector<ll> sqrs[20];
unordered_set<ll> us;
map<string,vector<int>> anagram;
ll mx=0;
int len(ll x) {
int ret=0;
while (x) {
ret++;
x/=10;
}
return ret;
}
void gen_sqr() {
for (int i=1;i<100000;i++) {
ll tmp=(ll)i*(ll)i;
us.insert(tmp);
sqrs[len(tmp)].pb(tmp);
}
}
bool match(const string& str,ll num) {
string cmp=to_string(num);
int tb[26];
memset(tb,-1,sizeof(tb));
for (int i=0;i<str.size();i++) {
char c=str[i];
char d=cmp[i];
if (tb[c-'A']==-1) {
tb[c-'A']=d-'0';
} else {
if (tb[c-'A']!=d-'0') return false;
}
}
return true;
}
void get_match() {
for (int i=0;i<vec.size();i++) {
int len=vec[i].size();
for (ll num:sqrs[len]) {
if (match(vec[i],num)) {
mt[i].pb(num);
}
}
}
}
bool same_digit(ll a,ll b) {
string sa=to_string(a);
string sb=to_string(b);
sort(sa.begin(),sa.end());
sort(sb.begin(),sb.end());
return sa==sb;
}
ll work(const string& a,const string& b,ll num) {
string c=to_string(num);
int tb[26];
memset(tb,-1,sizeof(tb));
bool vis[10];
memset(vis,0,sizeof(vis));
for (int i=0;i<a.size();i++) {
char p=a[i];
char q=c[i];
if (vis[q-'0']) {
return 2;
}
vis[q-'0']=1;
if (tb[p-'A']==-1) {
tb[p-'A']=q-'0';
}
}
ll ret=0;
for (int i=0;i<b.size();i++) {
int digit=tb[b[i]-'A'];
if (i==0&&digit==0) return 2;
ret=ret*10+digit;
}
return ret;
}
int main() {
freopen("input.txt","r",stdin);
gen_sqr();
while (~scanf("%s",buf)) {
vec.pb(buf);
sort(buf,buf+strlen(buf));
anagram[buf].pb(vec.size()-1);
memset(buf,0,sizeof(buf));
}
get_match();
for (auto& entry:anagram) {
vector<int>& v=entry.second;
for (int i=0;i<v.size();i++) {
for (int j=i+1;j<v.size();j++) {
vector<ll>& x=mt[v[i]];
for (ll ele:x) {
ll tmp=work(vec[v[i]],vec[v[j]],ele);
if (us.find(tmp)!=us.end()) {
mx=max(mx,tmp);
mx=max(mx,ele);
printf("%s,%lld %s,%lld\n",vec[v[i]].c_str(),ele,vec[v[j]].c_str(),tmp);
}
}
}
}
}
printf("%lld\n",mx);
}