1. 程式人生 > >Push Button I

Push Button I

題目1 : Push Button I

時間限制:5000ms

單點時限:1000ms

記憶體限制:256MB

描述

There are N buttons on the console. Each button needs to be pushed exactly once. Each time you may push several buttons simultaneously.

Assume there are 4 buttons. You can first push button 1 and button 3 at one time, then push button 2 and button 4 at one time. It can be represented as a string "13-24". Other pushing ways may be "1-2-4-3", "23-14" or "1234". Note that "23-41" is the same as "23-14".

Given the number N your task is to find all the pushing ways.

輸入

An integer N. (1 <= N <= 8)

輸出

Output the different pushing ways in lexicographical order.

For the same pushing way output the smallest string in lexicographical order as representative.

 

樣例輸入

3

樣例輸出

1-2-3
1-23
1-3-2
12-3
123
13-2
2-1-3
2-13
2-3-1
23-1
3-1-2
3-12
3-2-1

大佬的程式碼:位運算玩的6

#include <iostream>
using namespace std;

int n;
void dfs(int st, string tmp, string ans, int last)
{
    if ( st == 0 && tmp.length() == 0 )
    {
        cout<<ans<<endl;
        return ;
    }
    for ( int i = 0 ; i < n ; i++ )
    {
        if ( st & (1<<i) )
        {
            if ( i > last )
                dfs(st^(1<<i), "", ans+char('1'+i) + ((st^(1<<i)) ? "-" : "" ), -1);
            if ( i > last )
                dfs(st^(1<<i), tmp+char('1'+i), ans+char('1'+i), i);
        }
    }
}
int main()
{
    cin>>n;
    dfs((1<<n)-1, "", "", -1);
    return 0;
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
import java.util.regex.Pattern;

/*
   生成初始值
   遞迴排列初始值並新增到容器中
   遍歷容器中的值迴圈新增分隔符
   將生成的新值進行分組排序新增到set中
   列印set中所有的值
    */
public class Main {
    int num;
    StringBuilder initString ;
    ArrayList<String> allString ;
    HashSet<String> answer;

    public Main(int num) {
        this.num = num;
        initString = new StringBuilder();
        allString = new ArrayList<>();
        this.InitString(num);
        this.AllString(initString.toString(), 1 , num);

    }

    public static void main(String[] arg){
        Scanner in = new Scanner(System.in);
        while (in.hasNext()){
            Main Main = new Main(in.nextInt());
            // Main.InitString(Integer.parseInt(s));
            ArrayList<String> results = Main.addMinus(Main.allString, Main.num);
            for (String str:results){
                System.out.println(str);
            }
        }

    }

    /**
     * 返回1-num的初始順序排列值
     * @param num
     * @return
     */
    private void InitString(int num){
        if (num > 0 && num < 10){
            InitString(1 , num);
        }
    }

    private void InitString(int start, int end) {
        for (int i = start; i <= end; i++) {
            initString.append(i + "");
        }
    }

    /**
     * 生成所有排列組合
     * @param str
     * @param m
     * @param n
     */
    private void AllString(String str, int m, int n) {
        if (m == n) {
            allString.add(str);
        } else {
            for (int i = m ; i <= n ; i++)
            {
                String temp = swap(str, m , i).toString();
                AllString(temp, m + 1, n);
            }
        }
    }

    /**
     * 交換m,i的字元
     * @param str
     * @param m
     * @param i
     * @return
     */
    public static StringBuilder swap(String str, int m, int i) {
        char a = str.charAt(m - 1);
        char b = str.charAt(i - 1);
        StringBuilder stringBuilder = new StringBuilder(str);
        stringBuilder.replace(m - 1, m, String.valueOf(b));
        stringBuilder.replace(i - 1, i, String.valueOf(a));
        return stringBuilder;
    }



    /**
     * 將字串分組排序
     * @param buttons
     * @return
     */
    private String Sort(String buttons){
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder temp = new StringBuilder();
        for (int i = 0; i < buttons.length(); i++){
            if (buttons.charAt(i) == '-') {
                for (int m = 0; m < temp.length() - 1; m++){
                    for (int n = m + 1; n < temp.length(); n++){
                        if (temp.charAt(m) > temp.charAt(n)){
                            temp = Main.swap(temp.toString(), m+1, n+1);
                        }
                    }
                }
                stringBuilder.append(temp + "-");
                temp.setLength(0);
            }else {
                temp.append(buttons.charAt(i));
                if (i == buttons.length() - 1 && buttons.length() > 0){
                    for (int m = 0; m < temp.length() - 1; m++){
                        for (int n = m + 1; n < temp.length(); n++){
                            if (temp.charAt(m) > temp.charAt(n)){
                                temp = Main.swap(temp.toString(), m+1, n+1);
                            }
                        }
                    }
                    stringBuilder.append(temp);
                }
            }
        }
        return stringBuilder.toString();
    }

    /**
     * 窮舉新增符號返回到HashSet
     * @param allString
     * @param num
     * @return
     */
    private ArrayList<String> addMinus(ArrayList<String> allString, int num){
        HashSet<String> temp = new HashSet<>();
        for (int i = 0; i < allString.size(); i++){
            for (int j = 0; j < Math.pow(2, num - 1); j++){
                StringBuilder stringBuilder = new StringBuilder(allString.get(i));
                for (int k = num - 1; k > 0; k--){
                    if (((j >> k - 1) & 1 )> 0)  {
                        stringBuilder.insert(k, "-");
                    }
                }

                temp.add(Sort(stringBuilder.toString()));
            }

        }

        ArrayList<String> results = new ArrayList<>(temp);
        Collections.sort(results);

        return results;
    }



}