ZOJ Problem Set - 1004 - Anagrams by Stack

import java.util.Scanner;

public class Main {
    private static char[] solution = new char[200];
    private static char[] s = new char[200];
    private static String source;
    private static String target;
    private static int top;
    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while(input.hasNext()) {
            top = 1;
            source = input.nextLine();
            target = input.nextLine();
            if(source.length() != target.length()) {
                System.out.println("[\n]");
                continue;
            }
            System.out.println("[");
            dfs(0, 0, 0);
            System.out.println("]");
        }
    }

    private static void dfs(int depth, int npush, int npop) {
        if(source.length() == npush && npush == npop) {
            for(int i = 0; i < 2*source.length(); i++) {
                if(i == 2*source.length()-1) {
                    System.out.println(solution[i] + " ");
                } else {
                    System.out.print(solution[i] + " ");
                }
            }
            return;
        }
        
        if(npush < source.length()) {
            s[top++] = source.charAt(npush);
            solution[depth] = 'i';
            dfs(depth+1, npush+1, npop);
            top--;
        }
        
        if(top > 0 && s[top - 1] == target.charAt(npop)) {
            solution[depth] = 'o';
            char temp = s[top-1];
            top--;
            dfs(depth+1, npush, npop+1);
            s[top++] = temp;
        }
        return;
    }
}

 

转载于:https://www.cnblogs.com/NEU-2015/p/8410901.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值