康托展开:对全排列的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开始计数)
康托展开的逆运算:就是根据某个排列的在总的排列中的名次来确定这个排列。比如:
求1234所有排列中排第20的是啥,那么就利用辗转相除法确定康托展开中的系数,然后每次输出当前未出现过的第
个元素。
代码实现康托展开逆运算:
那么对于这题,我们就暴力模拟,然后将状态数压缩到362880
就不用开10的9次幂那么大的数组了
具体实现如下,由于我为了方便用了string,于是常数被卡了,这题还是要用int表示状态快
附上AC题解http://blog.csdn.net/liangzhaoyang1/article/details/53471515
前置技能:(千万注意是从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 | ||||||
| ||||||
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 |
就不用开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
相关文章推荐
- 给定M*N矩阵,每一行、每一列都按升序排列,判断是否存在某个元素。
- 假定一个解并判断是否可行(二分搜索应用)
- 判断短字符串中的所有字符是否在长字符串中全部出现
- 字符串问题---判断字符数组中是否所有的字符只出现了一次
- 判断图中是否出现“环”的算法
- hash_equals 判断 字符串值是否相等
- 判断后序遍历尔查搜索树是否正确
- 判断模块连接redis服务器是否出现异常
- 判断一个字符串中的字符是否都在另一个中出现
- 判断短字符串中的每个字母是否都在长字符串中出现过
- unity 怎么判断凹凸不平地面 物体是否出现在视野之内
- HDU 4618 Palindrome Sub-Array 二分(奇偶)+hash判断是否对称
- 判断网站是否被搜索降权的技术误区
- 判断 是否 出现滚动条
- js判断来路是否是百度等搜索索引进行弹窗或自动跳转
- 判断一棵二叉树是否为搜索二叉树、完全二叉树、平衡二叉树(java)
- indexOf() 如何判断一个元素在指定数组中是否存在? 找出指定元素出现的所有位置? indexOf()方法 是正序查找,lastIndexOf()是倒叙查找
- 在一个二维数组中,每一行都按照从左到右递增的顺序排列,每一列都按照从上到下递增的顺序排列,请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
- 在一个二维数组中,每一行都按照从左到右递增的顺序排列,每一列都按照从上到下递增的顺序排列,请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
- 判断字符串中是否出现了以下字符串(汉字)