This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAX_N = 26*26*26*26 + 5;
int n;
char C[MAX_N][4]; // cards
int cnt[MAX_N];
int L[676], R[676], U[676], D[676];
void Read(char IN[])
{
scanf(" %c%c %c%c", &IN[0], &IN[1], &IN[2], &IN[3]);
}
int Hash2(char a, char b)
{
return 26 * (a - 'A') + (b - 'A');
}
int Hash4(char Arr[4])
{
int h = 0;
for(int i = 0; i < 4; i++)
{
h *= 26;
h += Arr[i] - 'A';
}
return h;
}
int Hash4(int a, int b, int c, int d)
{
int h = a;
h *= 26;
h += b;
h *= 26;
h += c;
h *= 26;
h += d;
return h;
}
int main()
{
scanf("%d", &n);
ll ans = 0;
for(int i = 0; i < n; i++)
{
Read(C[i]);
L[Hash2(C[i][0], C[i][2])]++;
R[Hash2(C[i][1], C[i][3])]++;
U[Hash2(C[i][0], C[i][1])]++;
D[Hash2(C[i][2], C[i][3])]++;
cnt[Hash4(C[i])]++;
//cout << C[i] << endl;
if(C[i][0] == C[i][2] && C[i][1] == C[i][3]) // counts himself
{
ans--;
}
if(C[i][0] == C[i][1] && C[i][2] == C[i][3]) // counts himself
{
ans--;
}
}
for(int i = 0; i <= 676; i++)
{
ans += (ll)L[i] * R[i];
ans += (ll)U[i] * D[i];
}
for(int i = 0; i < 26; i++) for(int j = 0; j < 26; j++)
{
for(int k = 0; k < 26; k++) for(int l = 0; l < 26; l++)
{
int h1, h2;
h1 = Hash4(i, j, k, l);
h2 = Hash4(k, l, i, j);
/*
IJ KL
KL IJ
*/
if(h1 < h2)
ans -= cnt[h1] * cnt[h2];
h2 = Hash4(j, i, l, k);
/*
IJ JI
KL LK
*/
if(h1 < h2)
ans -= cnt[h1] * cnt[h2];
h1 = Hash4(i, j, k, i), h2 = Hash4(j, l, i, j);
/*
IJ JL
KI IJ
*/
if(h1 != h2)
ans -= cnt[h1] * cnt[h2];
h1 = Hash4(k, i, i, j), h2 = Hash4(i, j, j, l);
/*
KI IJ
IJ JL
*/
if(h1 != h2)
ans -= cnt[h1] * cnt[h2];
}
/*
IJ JI
JI IJ
*/
if(i < j)
{
ans += 3 * cnt[Hash4(i, j, j, i)] * cnt[Hash4(j, i, i, j)];
}
}
cout << ans << endl;
return 0;
}
/* INPUT
3
IO
OI
AA
AA
OI
IO
*/
Compilation message (stderr)
korteles.cpp: In function 'void Read(char*)':
korteles.cpp:17:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
17 | scanf(" %c%c %c%c", &IN[0], &IN[1], &IN[2], &IN[3]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
korteles.cpp: In function 'int main()':
korteles.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
50 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
korteles.cpp:75:23: warning: iteration 676 invokes undefined behavior [-Waggressive-loop-optimizations]
75 | ans += (ll)L[i] * R[i];
| ~~~^
korteles.cpp:73:22: note: within this loop
73 | for(int i = 0; i <= 676; i++)
| ~~^~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |