您的位置:首页 > 其它

康托展开:对全排列的HASH和还原,判断搜索中的某个排列是否出现过

2017-04-28 17:30 190 查看
题目:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2297

前置技能:(千万注意是从0开始数的

康托展开表示的是当前排列在n个不同元素的全排列中的名次。比如213在这3个数所有排列中排第3。

那么,对于n个数的排列,康托展开为:


其中

表示第i个元素在未出现的元素中排列第几。举个简单的例子:

对于排列4213来说,4在4213中排第3,注意从0开始,2在213中排第1,1在13中排第0,3在3中排第0,即:


,这样得到4213在所有排列中排第ans=20

代码实现:(从0开始计数)

//康托展开
LL Work(char str[])
{
int len = strlen(str);
LL ans = 0;
for(int i=0; i<len; i++)
{
int tmp = 0;
for(int j=i+1; j<len; j++)
if(str[j] < str[i]) tmp++;
ans += tmp * f[len-i-1];  //f[]为阶乘
}
return ans;  //返回该字符串是全排列中第几大,从1开始
}


康托展开的逆运算:就是根据某个排列的在总的排列中的名次来确定这个排列。比如:

求1234所有排列中排第20的是啥,那么就利用辗转相除法确定康托展开中的系数,然后每次输出当前未出现过的第

个元素。

代码实现康托展开逆运算:

//康托展开逆运算
void Work(LL n,LL m)
{
n--;
vector<int> v;
vector<int> a;
for(int i=1;i<=m;i++)
v.push_back(i);
for(int i=m;i>=1;i--)
{
LL r = n % f[i-1];
LL t = n / f[i-1];
n = r;
sort(v.begin(),v.end());
a.push_back(v[t]);
v.erase(v.begin()+t);
}
vector<int>::iterator it;
for(it = a.begin();it != a.end();it++)
cout<<*it;
cout<<endl;
}


Nine Digits
Time Limit: 3000 MSMemory Limit: 32768 K
Total Submit: 69(19 users)Total Accepted: 16(12 users)Rating:







Special Judge: No
Description

Input
多组数据,每组测试数据输入9个整数,为1-9的一个全排列。初始状态会被描述为

1 2 3 4 5 6 7 8 9

Output
输出所需要的最小移动步数。

Sample Input
1 2 3 4 5 6 7 8 9

9 8 7 6 5 4 3 2 1

Sample Output
0

12

那么对于这题,我们就暴力模拟,然后将状态数压缩到362880

就不用开10的9次幂那么大的数组了

具体实现如下,由于我为了方便用了string,于是常数被卡了,这题还是要用int表示状态快

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=362880;
char s[10];
int f[10];
int cantor(string s){
int ans=0;
for(int i=0;i<s.size();++i){
int tmp=0;
for(int j=i+1;j<s.size();++j)
if(s[i]>s[j]) tmp++;
ans+=tmp*f[s.size()-i-1];
}
return ans;
}
void LU(string &s){
char t[10];
t[0]=s[0];t[1]=s[1];t[3]=s[3];t[4]=s[4];
s[0]=t[3];s[1]=t[0];s[3]=t[4];s[4]=t[1];
}
void RU(string &s){
char t[10];
t[1]=s[1];t[2]=s[2];t[4]=s[4];t[5]=s[5];
s[1]=t[4];s[2]=t[1];s[4]=t[5];s[5]=t[2];
}
void LD(string &s){
char t[10];
t[3]=s[3];t[4]=s[4];t[6]=s[6];t[7]=s[7];
s[3]=t[6];s[4]=t[3];s[6]=t[7];s[7]=t[4];
}
void RD(string &s){
char t[10];
t[4]=s[4];t[5]=s[5];t[7]=s[7];t[8]=s[8];
s[4]=t[7];s[5]=t[4];s[7]=t[8];s[8]=t[5];
}
string temp[4];int step[maxn];bool vis[maxn];
void bfs(){
memset(step,-1,sizeof(step));
memset(vis,0,sizeof(vis));
queue<string> Q;Q.push(string(s));step[cantor(string(s))]=0;
string end=string("123456789");
while(Q.size()){
string now=Q.front();Q.pop();int nstate=cantor(now);
if(now.compare(end)==0) break;
for(int i=0;i<4;++i) temp[i]=now;
LU(temp[0]);RU(temp[1]);LD(temp[2]);RD(temp[3]);
for(int i=0;i<4;++i){
int state=cantor(temp[i]);
if(!vis[state]){
vis[state]=1;Q.push(temp[i]);step[state]=step[nstate]+1;
}
}
}
}
int a[10];
int main(){
f[1]=1;
for(int i=2;i<=9;++i){
f[i]=i*f[i-1];
}
while(~scanf("%d",a)){
for(int i=1;i<9;++i) scanf("%d",a+i);
for(int i=0;i<9;++i) s[i]=a[i]+'0';s[9]='\0';
bfs();
printf("%d\n",step[0]);
}
return 0;
}


附上AC题解http://blog.csdn.net/liangzhaoyang1/article/details/53471515
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐