通过以任意顺序连接 N 个二进制字符串,可能的反转对的最小计数
给定N个数组str形式的字符串,每个字符串的长度为M且仅包含字符“ a ”和“ b ”。任务是在不改变任何单个字符串的顺序的情况下,在通过以任何顺序连接所有N个字符串形成的结果字符串中找到可能的最小反转对数的计数。
An Inversion Pair in a string S is a pair of indices (i,j) such that i
For example, the string S= “abababbbb” contains 3 inversions : (2,3), (2,5), (4,5).
例子:
Input: N = 3 , M = 2, str = {“ba” , “aa” , “ab”}
Output: 2
Explanation: If we concatenate the strings in order s2 + s1 + s3 = “aabaab” , then the inversion pair will be at (3, 4) and (3 , 5)
Input: N = 2 , M = 2, str = {“b” , “b”}
Output: 0
方法:这个问题可以用贪心算法来解决 并且方法应该是在右端保留具有较高字符' b '计数的字符串。
请按照以下步骤解决问题:
- 取一个大小为n的字符串向量来接收输入。
- 使用基于特定字符串中“ b ”计数的比较器对字符串向量进行排序。
- 取一个空字符串,让我们说res = “”。
- 在遍历排序后的向量之后,将当前字符串添加到字符串res 中。
- 遍历字符串res并保持'b'的出现次数,每当遇到字符'a'时,将到目前为止看到的'b'的数量添加到ans变量中,因为使用该'a'你可以将配对组成为 a到目前为止看到的“b”的数量。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Comparator function to sort the vectors
// of strings
bool cmp(string& s1, string& s2)
{
return count(s1.begin(), s1.end(), 'b')
< count(s2.begin(), s2.end(), 'b');
}
// Function to find the inversion pairs
int findInversion(vector arr, int N, int M)
{
// Sort the vector
sort(arr.begin(), arr.end(), cmp);
string res = "";
// Concatenate the strings
for (int i = 0; i < N; i++) {
res = res + arr[i];
}
// Count number of 'b'
int cnt = 0;
// Count the total number of
// inversion pairs in the entire
int inversionPairs = 0;
// N*M = new size of the concatenated string
for (int i = 0; i < N * M; i++) {
// If the current character is 'b'
// then increase the count of cnt
if (res[i] == 'b') {
cnt += 1;
continue;
}
// Add the cnt because that number of
// pairs can be formed to that that 'a'
else {
inversionPairs = inversionPairs + cnt;
}
}
return inversionPairs;
}
// Driver Function
int main()
{
int N = 3, M = 2;
vector arr = { "ba" , "aa" , "ab" };
// Calling the function
int ans = findInversion(arr, N, M);
// Printing the answer
cout << ans << "\n";
return 0;
}
Python3
# Python program for the above approach
# Comparator function to sort the vectors
# of strings
def cmp(s):
s1 = s[0]
s2 = s[1]
b_in_s1 = 0
b_in_s2 = 0
for i in s1:
if (i == 'b'):
b_in_s1 += 1
for i in s2:
if (i == 'b'):
b_in_s2 += 1
if(b_in_s1 == b_in_s2):
return 0
return 1 if b_in_s1 - b_in_s2 else -1;
# Function to find the inversion pairs
def findInversion(arr, N, M):
# Sort the vector
#arr.sort(key=cmp);
arr.sort(key=cmp)
res = "";
# Concatenate the strings
for i in range(N):
res = res + arr[i];
# Count number of 'b'
cnt = 0;
# Count the total number of
# inversion pairs in the entire
inversionPairs = 0;
# N*M = new size of the concatenated string
for i in range(N * M):
# If the current character is 'b'
# then increase the count of cnt
if (res[i] == 'b'):
cnt += 1;
continue;
# Add the cnt because that number of
# pairs can be formed to that that 'a'
else:
inversionPairs = inversionPairs + cnt;
return inversionPairs;
# Driver Function
N = 3
M = 2;
arr = ["ba", "aa", "ab"];
# Calling the function
ans = findInversion(arr, N, M);
# Printing the answer
print(ans);
# This code is contributed by saurabh_jaiswal.
Javascript
输出
2
时间复杂度: O(NlogN)
辅助空间: O(N*M)