题意:有n个雪花,每片雪花6个长度,当且仅当两片雪花对应位置的数字相同时为相同的雪花,雪花可以顺时针旋转、翻折。
比如1 2 3 4 5 6 和 1 6 5 4 3 2 或者 2 3 4 5 6 1是相同的雪花。
思路:把每片雪花的顺时针序和逆时针序共12个取字典序最小的存下来。然后只要sort一下n个雪花序列然后找有没有相同的即可。
/*
time:2532ms
memory:3000kB
*/
#include<iostream>
#include<string>
#include<cstring>
#include<stdlib.h>
#include<cstdio>
#include<stdio.h>
#include<set>
#include<map>
#include<deque>
#include<stack>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<list>
#include<bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxint = ~0U >> 1;
const int maxn = 100010;
int tmp[6];
struct snow
{
public:
int a[6];
snow(int* x)
{
snow tmp;bool ok = false;
memset(a, 0x3f, sizeof a);
for (int i = 0;i < 6;i++)
{
for (int j = 0;j < 6;j++)//顺时针序
tmp.a[j] = x[(i + j) % 6];
if (tmp < *this)
*this = tmp;
for (int j = 0;j < 6;j++)//逆时针序
tmp.a[j] = x[(i - j + 6) % 6];
if (tmp < *this)
*this = tmp;
}
}
snow() {}
friend bool operator <(const snow& a,const snow& b)//按字典序排序
{
int t = 0;
while (a.a[t] == b.a[t] && t < 5)t++;
return a.a[t] < b.a[t];
}
bool operator ==(const snow& b)
{
for (int i = 0;i < 6;i++)
if (a[i] != b.a[i])return false;
return true;
}
}s[maxn];
int main()
{
int n;
bool ok = false;
scanf("%d", &n);
for (int i = 0;i < n;i++)
{
for (int j = 0;j < 6;j++)
scanf("%d", tmp + j);
s[i] = snow(tmp);
}
sort(s, s + n);
for (int i = 0;i < n - 1;i++)
{
if (s[i] == s[i + 1])
{
ok = true;
break;
}
}
if(ok)
printf("Twin snowflakes found.\n");
else
printf("No two snowflakes are alike.\n");
return 0;
}
/****************************************更新线*********************************************/
更新:使用了快速读入后时间压缩至719ms
#include<iostream>
#include<string>
#include<cstring>
#include<stdlib.h>
#include<cstdio>
#include<stdio.h>
#include<set>
#include<map>
#include<deque>
#include<stack>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<list>
#include<bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxint = ~0U >> 1;
char * cp = (char *)malloc(20000000);
inline void in(int &x) {
while (*cp<'0' || *cp>'9')++cp;
for (x = 0;*cp >= '0'&&*cp <= '9';)x = x * 10 + (*cp++^'0');
}
const int maxn = 100010;
int tmp[6];
struct snow
{
public:
int a[6];
snow(int* x)
{
snow tmp;bool ok = false;
memset(a, 0x3f, sizeof a);
for (int i = 0;i < 6;i++)
{
for (int j = 0;j < 6;j++)
tmp.a[j] = x[(i + j) % 6];
if (tmp < *this)
*this = tmp;
for (int j = 0;j < 6;j++)
tmp.a[j] = x[(i - j + 6) % 6];
if (tmp < *this)
*this = tmp;
}
}
snow() {}
friend bool operator <(const snow& a,const snow& b)
{
int t = 0;
while (a.a[t] == b.a[t] && t < 5)t++;
return a.a[t] < b.a[t];
}
bool operator ==(const snow& b)
{
for (int i = 0;i < 6;i++)
if (a[i] != b.a[i])return false;
return true;
}
}s[maxn];
int main()
{
int n;
fread(cp, 1, 20000000, stdin);
bool ok = false;
in(n);
for (int i = 0;i < n;i++)
{
for (int j = 0;j < 6;j++)
in(tmp[j]);
s[i] = snow(tmp);
}
sort(s, s + n);
for (int i = 0;i < n - 1;i++)
{
if (s[i] == s[i + 1])
{
ok = true;
break;
}
}
if(ok)
printf("Twin snowflakes found.\n");
else
printf("No two snowflakes are alike.\n");
return 0;
}