HDU 神奇的花瓣
Problem Description
ZZC学长很具有科研精神,他来自遥远的内蒙古草原,对西安的很多事物都十分好奇。
今天,他又在宿舍一楼阳台下的阴暗角落里发现了一种奇异的花。这种花由六瓣组成,每瓣形状各异。
ZZC学长统计出花瓣共有六种形状,并为这些形状编号。他找到了N朵花,以顺时针方向对每朵花进行观察,但是他的数学跟ZKY学长一样不是很好,你能帮他统计一共有多少朵形状不同的花么?
(hint:123456和612345是一种形状的花)
Input
多组输入,每组第一行数字N,第二行开始N行每行六个数字表示一朵花的形状。(1<=N<=1000000)
Output
对于每组输入,在单独的一行输出一个数,表示多少种形状不同的花。
Sample Input
2
123456
612345
3
135555
124444
355551
Sample Output
1
2
思路:用map来存,注意优化不然会超时。
下面是代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <list>
const int maxn=5e2+10;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
string s,p;
int flag;
map<ll,int> m;
int main()
{
ll t;
ios::sync_with_stdio(false);//取消cin于stdin的同步,不然用cin会读的很慢
int n;
while(cin>>n)
{
m.clear();
char ch;
int num=0;
for(int i=1;i<=n;i++)
{
flag = 1;
cin >> s;
ch = s[0];
for(int k=1;k<6;k++)//找最小数字
{
if(ch>s[k])
ch = s[k];
}
for(int k=0;k<6;k++)//6种状态
{
if(s[k]!=ch)//优化(找以最小数字开头的状态,可以省略很多情况)
continue;
p.clear();
for(int j=k;j<6;j++)
{
p += s[j];
}
for(int j=0;j<k;j++)
{
p += s[j];
}
t = atoi(p.c_str());
if(m[t])//如果存在
{
flag = 0;
break;
}
}
if(flag)//如果不存在
{
m[t]++;
num++;
}
}
cout <<num<<endl;
}
return 0;
}