String 更改字符串使其相等

String 更改字符串使其相等,string,algorithm,String,Algorithm,关于问题 我们有两个字符串A和B,它们具有相同的超级字符集。我们 需要更改这些字符串以获得两个相等的字符串。一举一动 我们可以执行以下操作之一: 1-交换字符串的两个连续字符 2-交换第一个和第二个 字符串的最后一个字符 可以对任一字符串执行移动。最低人数是多少 为了得到两个相等的字符串,我们需要移动多少?输入 格式和约束:输入的第一行和第二行 包含两个字符串A和B。保证超集 字符是相等的。1您有两个原子操作: 与成本1互换 先交换后交换,成本为1 一个有趣的事实: 二,。如果字符串结束将附加到

关于问题

我们有两个字符串A和B,它们具有相同的超级字符集。我们 需要更改这些字符串以获得两个相等的字符串。一举一动 我们可以执行以下操作之一:

1-交换字符串的两个连续字符
2-交换第一个和第二个 字符串的最后一个字符

可以对任一字符串执行移动。最低人数是多少 为了得到两个相等的字符串,我们需要移动多少?输入 格式和约束:输入的第一行和第二行 包含两个字符串A和B。保证超集
字符是相等的。1您有两个原子操作:

  • 与成本1互换

  • 先交换后交换,成本为1

  • 一个有趣的事实:

  • 二,。如果字符串结束将附加到字符串开始(圆形字符串),则相同
  • 所以我们可以推导出一个更一般的运算

  • 将成本=|从-移动到|的字符(跨边界)
  • 这个问题对我来说似乎不是二维的,或者说我无法确定维度。将此算法视为天真的方法:

    private static int transform(String from, String to) {
        int commonLength = to.length();
        List<Solution> worklist = new ArrayList<>();
        worklist.add(new Solution(0,from));
        while (!worklist.isEmpty()) {
            Solution item = worklist.remove(0);
            if (item.remainder.length() == 0) {
                return item.cost;
            } else {
                int toPosition = commonLength - item.remainder.length();
                char expected = to.charAt(toPosition);
                nextpos : for (int i = 0; i < item.remainder.length(); i++) {
                    if (item.remainder.charAt(i) == expected) {
                        Solution nextSolution = item.moveCharToBegin(i, commonLength);
                        for (Solution solution : worklist) {
                            if (solution.remainder.equals(nextSolution.remainder)) {
                                solution.cost = Math.min(solution.cost, nextSolution.cost);
                                continue nextpos;
                            }
                        }
                        worklist.add(nextSolution);
                    }
                }
            }
        }
        return Integer.MAX_VALUE;
    }
    
    private static class Solution {
        public int cost;
        public String remainder;
    
        public Solution(int cost, String remainder) {
            this.cost = cost;
            this.remainder = remainder;
        }
    
        public Solution moveCharToBegin(int i, int length) {
            int costOffset = Math.min(i, length - i); //minimum of forward and backward circular move
            String newRemainder = remainder.substring(0, i) + remainder.substring(i + 1);
            return new Solution(cost + costOffset, newRemainder);
        }
    }
    
    私有静态int转换(字符串从,字符串到){
    int commonLength=to.length();
    列表工作列表=新建ArrayList();
    添加(新解决方案(0,从));
    而(!worklist.isEmpty()){
    解决方案项=工作列表。删除(0);
    if(item.rements.length()==0){
    退货项目成本;
    }否则{
    int-toPosition=commonLength-item.rements.length();
    char预期=to.charAt(位置);
    nextpos:for(int i=0;i >我认为不可能使用DP,至少在这个解决方案中是不可能的,因为不能用中间元素中的一个元素来改变最后一个元素的位置。
    
    private static int transform(String from, String to) {
        int commonLength = to.length();
        List<Solution> worklist = new ArrayList<>();
        worklist.add(new Solution(0,from));
        while (!worklist.isEmpty()) {
            Solution item = worklist.remove(0);
            if (item.remainder.length() == 0) {
                return item.cost;
            } else {
                int toPosition = commonLength - item.remainder.length();
                char expected = to.charAt(toPosition);
                nextpos : for (int i = 0; i < item.remainder.length(); i++) {
                    if (item.remainder.charAt(i) == expected) {
                        Solution nextSolution = item.moveCharToBegin(i, commonLength);
                        for (Solution solution : worklist) {
                            if (solution.remainder.equals(nextSolution.remainder)) {
                                solution.cost = Math.min(solution.cost, nextSolution.cost);
                                continue nextpos;
                            }
                        }
                        worklist.add(nextSolution);
                    }
                }
            }
        }
        return Integer.MAX_VALUE;
    }
    
    private static class Solution {
        public int cost;
        public String remainder;
    
        public Solution(int cost, String remainder) {
            this.cost = cost;
            this.remainder = remainder;
        }
    
        public Solution moveCharToBegin(int i, int length) {
            int costOffset = Math.min(i, length - i); //minimum of forward and backward circular move
            String newRemainder = remainder.substring(0, i) + remainder.substring(i + 1);
            return new Solution(cost + costOffset, newRemainder);
        }
    }