📌  相关文章
📜  使所有字符串相等的最小移动到结束操作

📅  最后修改于: 2022-05-13 01:57:07.131000             🧑  作者: Mango

使所有字符串相等的最小移动到结束操作

给定 n 个相互排列的字符串。我们需要通过一个将任何字符串的前面字符移到末尾的操作来使所有字符串相同。
例子:

Input : n = 2
        arr[] = {"molzv", "lzvmo"}
Output : 2
Explanation: In first string, we remove
first element("m") from first string and 
append it end. Then we move second character
of first string and move it to end. So after
2 operations, both strings become same.

Input : n = 3
        arr[] = {"kc", "kc", "kc"}
Output : 0
Explanation: already all strings are equal.

移动到结束操作基本上是左旋转。我们使用检查字符串是否相互旋转中讨论的方法来计算使两个字符串相同所需的移到前面操作的次数。我们一一考虑每个字符串作为目标字符串。我们计算使所有其他字符串与当前目标相同所需的旋转,并最终返回所有计数的最小值。
下面是上述方法的实现。

C++
// CPP program to make all strings same using
// move to end operations.
#include 
using namespace std;
 
// Returns minimum number of moves to end
// operations to make all strings same.
int minimunMoves(string arr[], int n)
{
    int ans = INT_MAX;
    for (int i = 0; i < n; i++)
    {
        int curr_count = 0; 
 
        // Consider s[i] as target string and
        // count rotations required to make
        // all other strings same as str[i].
        for (int j = 0; j < n; j++) {
 
            string tmp = arr[j] + arr[j];
 
            // find function returns the index where we
            // found arr[i] which is actually count of
            // move-to-front operations.
            int index = tmp.find(arr[i]);
 
            // If any two strings are not rotations of
            // each other, we can't make them same. 
            if (index == string::npos)
                return -1;
 
            curr_count += index;
        }
 
        ans = min(curr_count, ans);
    }
 
    return ans;
}
 
// driver code for above function.
int main()
{
    string arr[] = {"xzzwo", "zwoxz", "zzwox", "xzzwo"}; 
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << minimunMoves(arr, n);
    return 0;
}


Java
// Java program to make all
// strings same using move
// to end operations.
import java.util.*;
class GFG
{
 
// Returns minimum number of
// moves to end operations
// to make all strings same.
static int minimunMoves(String arr[], int n)
{
    int ans = Integer.MAX_VALUE;
    for (int i = 0; i < n; i++)
    {
        int curr_count = 0;
 
        // Consider s[i] as target
        // string and count rotations
        // required to make all other
        // strings same as str[i].
        String tmp = "";
        for (int j = 0; j < n; j++)
        {
            tmp = arr[j] + arr[j];
 
            // find function returns the
            // index where we found arr[i]
            // which is actually count of
            // move-to-front operations.
            int index = tmp.indexOf(arr[i]);
 
            // If any two strings are not
            // rotations of each other,
            // we can't make them same.
            if (index != -1)
                curr_count += index;
            else
                curr_count = -1; 
        }
 
        ans = Math.min(curr_count, ans);
    }
 
    return ans;
}
 
// Driver code
public static void main(String args[])
{
    String arr[] = {"xzzwo", "zwoxz",
                    "zzwox", "xzzwo"};
    int n = arr.length;
    System.out.println(minimunMoves(arr, n));
}
}
 
// This code is contributed
// by Kirti_Mangal


Python 3
# Python 3 program to make all strings
# same using move to end operations.
import sys
 
# Returns minimum number of moves to end
# operations to make all strings same.
def minimunMoves(arr, n):
 
    ans = sys.maxsize
    for i in range(n):
 
        curr_count = 0
 
        # Consider s[i] as target string and
        # count rotations required to make
        # all other strings same as str[i].
        for j in range(n):
 
            tmp = arr[j] + arr[j]
 
            # find function returns the index where
            # we found arr[i] which is actually
            # count of move-to-front operations.
            index = tmp.find(arr[i])
 
            # If any two strings are not rotations of
            # each other, we can't make them same.
            if (index == len(arr[i])):
                return -1
 
            curr_count += index
 
        ans = min(curr_count, ans)
 
    return ans
 
# Driver Code
if __name__ == "__main__":
     
    arr = ["xzzwo", "zwoxz", "zzwox", "xzzwo"]
    n = len(arr)
    print( minimunMoves(arr, n))
 
# This code is contributed by ita_c


C#
using System;
 
// C# program to make all
// strings same using move
// to end operations.
public class GFG
{
 
// Returns minimum number of
// moves to end operations
// to make all strings same.
public  static int minimunMoves(string[] arr, int n)
{
    int ans = int.MaxValue;
    for (int i = 0; i < n; i++)
    {
        int curr_count = 0;
 
        // Consider s[i] as target
        // string and count rotations
        // required to make all other
        // strings same as str[i].
        string tmp = "";
        for (int j = 0; j < n; j++)
        {
            tmp = arr[j] + arr[j];
 
            // find function returns the
            // index where we found arr[i]
            // which is actually count of
            // move-to-front operations.
            int index = tmp.IndexOf(arr[i], StringComparison.Ordinal);
 
            // If any two strings are not
            // rotations of each other,
            // we can't make them same.
            if (index == arr[i].Length)
            {
                return -1;
            }
 
            curr_count += index;
        }
 
        ans = Math.Min(curr_count, ans);
    }
 
    return ans;
}
 
// Driver code
public static void Main(string[] args)
{
    string[] arr = new string[] {"xzzwo", "zwoxz", "zzwox", "xzzwo"};
    int n = arr.Length;
    Console.WriteLine(minimunMoves(arr, n));
}
}
 
// This code is contributed by Shrikant13


Javascript


输出:

5