寻找需要做2串等于最小移动最小

2023-09-11 03:01:43 作者:看着星星想着你

这是从网上编码的挑战(已完成)中的一个问题。 我只是需要一些逻辑,这是要如何处理。

问题描述: 我们有两个字符串A和B具有相同的超集的字符。我们需要改变这些字符串以获得两个相等的字符串。在每次移动我们可以执行以下操作之一:

  1。交换两个连续字符的字符串
2.互换,第一和字符串的最后一个字符
 

一个举动可以串上执行。 在最低是什么的,我们需要以获得两个相等的串动作多少?

输入格式和约束: 第一和输入的第二行包含两个串A和B这是保证的超集的字符相等的。

  1< =长度(A)=长度(B)< = 2000
所有的输入字符'a'和'z'的范围
 
互联网加养老 新模式,你知道多少

输出格式: 打印最小的移动号码到输出的唯一行

 样品输入:
AAB
咩

示例输出:
1
 

说明: 交换串AAB将其转换为BAA的第一和最后一个字符。两个字符串现在等于。

编辑:这是我第一次尝试,但我发现了错误的输出。有人可以指导我什么是错误的,我的做法。

INT minStringMoves(字符*一,字符* B){     INT长,POS机,I,J,移动= 0;     字符* PTR;     长度= strlen的(一);     对于(i = 0; I<长度;我++){         //找到b的第一次出现的[i]于一         PTR =和strchr(A,B [I]);         POS = PTR - 一个;         //如果它的最后一个元素,换用第一         如果(我== 0安培;&安培; POS ==长度-1){             交换(安培;一个[0],&安培;一个[长度-1]);             移动++;         }         //从当前索引到POS否则交换         其他 {             为(J = POS; J>我; j--){                 掉期(安培;一个[J],和放大器;一个[J-1]);                 移动++;             }         }         //如果相等,休息         如果(的strcmp(A,B)== 0)             打破;    }    返回的动作; }

解决方案

看看这个例子:

  aaaaaaaaab
abaaaaaaaa
 

您的解决方案:8

  aaaaaaaaab  - > aaaaaaaaba  - > aaaaaaabaa  - > aaaaaabaaa  - > aaaaabaaaa  - >
aaaabaaaaa  - > aaabaaaaaa  - > aabaaaaaaa  - > abaaaaaaaa
 

妥善解决:2

  aaaaaaaaab  - > baaaaaaaaa  - > abaaaaaaaa
 

您应该检查,如果换在其他方向上会给你更好的结果。

但有时你也会毁了字符串的previous一部分。例如:

  caaaaaaaab
cbaaaaaaaa

caaaaaaaab  - > baaaaaaaac  - > abaaaaaaac
 

您需要另一个交换这里放回'c'来的第一个地方。

正确的算法可能更复杂,但你现在可以看到什么不对您的解决方案。

This is a question from one of the online coding challenge (which has completed). I just need some logic for this as to how to approach.

Problem Statement: We have two strings A and B with the same super set of characters. We need to change these strings to obtain two equal strings. In each move we can perform one of the following operations:

1. swap two consecutive characters of a string  
2. swap the first and the last characters of a string

A move can be performed on either string. What is the minimum number of moves that we need in order to obtain two equal strings?

Input Format and Constraints: The first and the second line of the input contains two strings A and B. It is guaranteed that the superset their characters are equal.

1 <= length(A) = length(B) <= 2000
All the input characters are between 'a' and 'z'

Output Format: Print the minimum number of moves to the only line of the output

Sample input:
aab
baa

Sample output:
1

Explanation: Swap the first and last character of the string aab to convert it to baa. The two strings are now equal.

EDIT : Here is my first try, but I'm getting wrong output. Can someone guide me what is wrong in my approach.

int minStringMoves(char* a, char* b) {
    int length, pos, i, j, moves=0;
    char *ptr;
    length = strlen(a);

    for(i=0;i<length;i++) {
        // Find the first occurrence of b[i] in a
        ptr = strchr(a,b[i]);
        pos = ptr - a;

        // If its the last element, swap with the first
        if(i==0 && pos == length-1) {
            swap(&a[0], &a[length-1]);
            moves++;
        }
        // Else swap from current index till pos
        else {
            for(j=pos;j>i;j--) {
                swap(&a[j],&a[j-1]);
                moves++;
            }
        }

        // If equal, break
        if(strcmp(a,b) == 0)
            break;       
   }

   return moves;
}

解决方案

Take a look at this example:

aaaaaaaaab
abaaaaaaaa

Your solution: 8

aaaaaaaaab -> aaaaaaaaba -> aaaaaaabaa -> aaaaaabaaa -> aaaaabaaaa -> 
aaaabaaaaa -> aaabaaaaaa -> aabaaaaaaa -> abaaaaaaaa

Proper solution: 2

aaaaaaaaab -> baaaaaaaaa -> abaaaaaaaa

You should check if swapping in the other direction would give you better result.

But sometimes you will also ruin the previous part of the string. eg:

caaaaaaaab
cbaaaaaaaa

caaaaaaaab -> baaaaaaaac -> abaaaaaaac

You need another swap here to put back the 'c' to the first place.

The proper algorithm is probably even more complex, but you can see now what's wrong in your solution.