一道又重复数字的全排列的问题,用set保存,然后直接回溯。这这种做法,效率比较低,没有去剪枝,从而像1,1,1,1,1这样的序列都需要回溯很多次。今天写了一个剪枝的。
c++ STL algorithm中有next_permutation可以直接调用,ACM的时候经常偷懒用这个,呵呵。
学以致用:poj 1731
http://acm.pku.edu.cn/JudgeOnline/problem?id=1731
- public class Permutation
- {
- private int[] a;
- public Permutation(int[] a)
- {
- this.a = a;
- }
- public boolean isOk(int b,int e){//判断是否重复
- if(b < e){
- for(int i = b; i < e; i++){
- if(a[i] == a[e])
- return false;
- }
- }
- return true;
- }
- public void permutation(int k){
- if(k >= a.length){
- print();
- }else{
- for(int i = k; i < a.length; i++){
- if(isOk(k,i)){
- swap(i,k);
- permutation( k+1 );
- swap(i,k);
- }
- }
- }
- }
- private void swap( int i, int k )
- {
- int temp = a[i];
- a[i] = a[k];
- a[k] = temp;
- }
- private void print()
- {
- for( int i = 0; i < a.length; i++ )
- {
- System.out.print(a[i] + " ");
- }
- System.out.println();
- }
- public static void main( String[] args )
- {
- Permutation p = new Permutation(new int[]{1,2,2,2});
- p.permutation( 0 );
- }
- }
public class Permutation
{
private int[] a;
public Permutation(int[] a)
{
this.a = a;
}
public boolean isOk(int b,int e){//判断是否重复
if(b < e){
for(int i = b; i < e; i++){
if(a[i] == a[e])
return false;
}
}
return true;
}
public void permutation(int k){
if(k >= a.length){
print();
}else{
for(int i = k; i < a.length; i++){
if(isOk(k,i)){
swap(i,k);
permutation( k+1 );
swap(i,k);
}
}
}
}
private void swap( int i, int k )
{
int temp = a[i];
a[i] = a[k];
a[k] = temp;
}
private void print()
{
for( int i = 0; i < a.length; i++ )
{
System.out.print(a[i] + " ");
}
System.out.println();
}
public static void main( String[] args )
{
Permutation p = new Permutation(new int[]{1,2,2,2});
p.permutation( 0 );
}
}
c++ STL algorithm中有next_permutation可以直接调用,ACM的时候经常偷懒用这个,呵呵。
学以致用:poj 1731
http://acm.pku.edu.cn/JudgeOnline/problem?id=1731
- #include<iostream>
- #include<algorithm>
- #include<vector>
- #include<string>
- using namespace std;
- string goods;
- vector<string> v;
- void swap(string &str,int i, int j){
- char temp = str[i];
- str[i] = str[j];
- str[j] = temp;
- }
- bool ok(int i, int j){
- if( i < j){
- for(int k = i; k < j; k++){
- if(goods[k] == goods[j])
- return false;
- }
- }
- return true;
- }
- void backtrack(int i){
- if(i >= goods.length()){
- v.push_back(goods);
- }
- for(int j = i; j < goods.length(); j++){
- if(ok(i,j)){
- swap(goods,i,j);
- backtrack(i + 1);
- swap(goods,i,j);
- }
- }
- }
- int main(){
- cin >> goods;
- backtrack(0);
- sort(v.begin(),v.end());
- for(int i = 0; i < v.size(); i++){
- cout << v[i] << endl;
- }
- }
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
string goods;
vector<string> v;
void swap(string &str,int i, int j){
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
bool ok(int i, int j){
if( i < j){
for(int k = i; k < j; k++){
if(goods[k] == goods[j])
return false;
}
}
return true;
}
void backtrack(int i){
if(i >= goods.length()){
v.push_back(goods);
}
for(int j = i; j < goods.length(); j++){
if(ok(i,j)){
swap(goods,i,j);
backtrack(i + 1);
swap(goods,i,j);
}
}
}
int main(){
cin >> goods;
backtrack(0);
sort(v.begin(),v.end());
for(int i = 0; i < v.size(); i++){
cout << v[i] << endl;
}
}