给定一个仅由两个小写字符组成的字符串和和两个数字和 .任务是打印给定字符串的字典序最小排列,使得子序列的计数是和是 .如果不存在这样的字符串,则打印“Impossible”(不带引号)。
例子:
Input: str = "yxxyx", p = 3, q = 3
Output: xyxyx
Input: str = "yxxy", p = 3, q = 2
Output: Impossible
方法:首先,通过归纳可以证明’x’的计数和’y’的计数的乘积应该等于任何给定字符串的’xy’和’yx’的子序列的计数之和.如果这不成立,那么答案是“不可能”,否则答案总是存在。
现在,对给定的字符串排序,使 ‘yx’ 的子序列的计数变为零。设nx为 ‘x’ 的计数, ny为 ‘y’ 的计数。让a和b分别是子序列 ‘xy’ 和 ‘yx’ 的计数,然后a = nx*ny和b = 0 。然后,从字符串的开头找到下一个 ‘y’ 的 ‘x’ 并交换两者,直到到达字符串。在每次交换中, a递减1 , b递增1 。重复此操作,直到达到 ‘yx’ 子序列的计数,即a变为p并且b变为q 。
下面是上述方法的实现:
C++
// CPP program to find lexicographically smallest
// string such that count of subsequence 'xy' and
// 'yx' is p and q respectively.
#include
using namespace std;
// function to check if answer exits
int nx = 0, ny = 0;
bool check(string s, int p, int q)
{
// count total 'x' and 'y' in string
for (int i = 0; i < s.length(); ++i) {
if (s[i] == 'x')
nx++;
else
ny++;
}
// condition to check existence of answer
if (nx * ny != p + q)
return 1;
else
return 0;
}
// function to find lexicographically smallest string
string smallestPermutation(string s, int p, int q)
{
// check if answer exist or not
if (check(s, p, q) == 1) {
return "Impossible";
}
sort(s.begin(), s.end());
int a = nx * ny, b = 0, i, j;
// check if count of 'xy' and 'yx' becomes
// equal to p and q respectively.
if (a == p && b == q) {
return s;
}
// Repeat until answer is found.
while (1) {
// Find index of 'x' to swap with 'y'.
for (i = 0; i < s.length() - 1; ++i) {
if (s[i] == 'x' && s[i + 1] == 'y')
break;
}
for (j = i; j < s.length() - 1; j++) {
if (s[j] == 'x' && s[j + 1] == 'y') {
swap(s[j], s[j + 1]);
a--; // 'xy' decrement by 1
b++; // 'yx' increment by 1
// check if count of 'xy' and 'yx' becomes
// equal to p and q respectively.
if (a == p && b == q) {
return s;
}
}
}
}
}
// Driver code
int main()
{
string s = "yxxyx";
int p = 3, q = 3;
cout<< smallestPermutation(s, p, q);
return 0;
}
Java
// Java program to find lexicographically
// smallest string such that count of
// subsequence 'xy' and 'yx' is p and
// q respectively.
import java.util.*;
class GFG
{
static int nx = 0, ny = 0;
static boolean check(String s,
int p, int q)
{
// count total 'x' and 'y' in string
for (int i = 0; i < s.length(); ++i)
{
if (s.charAt(i) == 'x')
nx++;
else
ny++;
}
// condition to check
// existence of answer
if ((nx * ny) != (p + q))
return true;
else
return false;
}
public static String smallestPermutation(String s,
int p, int q)
{
if (check(s, p, q) == true)
{
return "Impossible";
}
char tempArray[] = s.toCharArray();
Arrays.sort(tempArray);
String str = new String(tempArray);
int a = nx * ny, b = 0, i = 0, j = 0;
if (a == p && b == q)
{
return str;
}
while (1 > 0)
{
// Find index of 'x' to swap with 'y'.
for (i = 0; i < str.length() - 1; ++i)
{
if (str.charAt(i) == 'x' &&
str.charAt(i + 1) == 'y')
break;
}
for (j = i; j < str.length() - 1; j++)
{
if (str.charAt(j) == 'x' &&
str.charAt(j + 1) == 'y')
{
StringBuilder sb = new StringBuilder(str);
sb.setCharAt(j, str.charAt(j + 1));
sb.setCharAt(j + 1, str.charAt(j));
str = sb.toString();
/* char ch[] = str.toCharArray();
char temp = ch[j+1];
ch[j+1] = ch[j];
ch[j] = temp;*/
a--; // 'xy' decrement by 1
b++; // 'yx' increment by 1
// check if count of 'xy' and
// 'yx' becomes equal to p
// and q respectively.
if (a == p && b == q)
{
return str;
}
}
}
}
}
// Driver Code
public static void main (String[] args)
{
String s = "yxxyx";
int p = 3, q = 3;
System.out.print(smallestPermutation(s, p, q));
}
}
// This code is contributed by Kirti_Mangal
Python3
# Python3 program to find lexicographically
# smallest string such that count of subsequence
# 'xy' and 'yx' is p and q respectively.
# Function to check if answer exits
def check(s, p, q):
global nx
global ny
# count total 'x' and 'y' in string
for i in range(0, len(s)):
if s[i] == 'x':
nx += 1
else:
ny += 1
# condition to check existence of answer
if nx * ny != p + q:
return 1
else:
return 0
# Function to find lexicographically
# smallest string
def smallestPermutation(s, p, q):
# check if answer exist or not
if check(s, p, q) == 1:
return "Impossible"
s = sorted(s)
a, b, i = nx * ny, 0, 0
# check if count of 'xy' and 'yx' becomes
# equal to p and q respectively.
if a == p and b == q:
return '' . join(s)
# Repeat until answer is found.
while True:
# Find index of 'x' to swap with 'y'.
for i in range(0, len(s) - 1):
if s[i] == 'x' and s[i + 1] == 'y':
break
for j in range(i, len(s) - 1):
if s[j] == 'x' and s[j + 1] == 'y':
s[j], s[j + 1] = s[j + 1], s[j]
a -= 1 # 'xy' decrement by 1
b += 1 # 'yx' increment by 1
# check if count of 'xy' and 'yx' becomes
# equal to p and q respectively.
if a == p and b == q:
return '' . join(s)
# Driver code
if __name__ == "__main__":
nx, ny = 0, 0
s = "yxxyx"
p, q = 3, 3
print(smallestPermutation(s, p, q))
# This code is contributed by Rituraj Jain
C#
// C# program to find lexicographically
// smallest string such that count of
// subsequence 'xy' and 'yx' is p and
// q respectively.
using System;
using System.Text;
class GFG
{
static int nx = 0, ny = 0;
static Boolean check(String s,
int p, int q)
{
// count total 'x' and 'y' in string
for (int i = 0; i < s.Length; ++i)
{
if (s[i] == 'x')
nx++;
else
ny++;
}
// condition to check
// existence of answer
if ((nx * ny) != (p + q))
return true;
else
return false;
}
public static String smallestPermutation(String s,
int p, int q)
{
if (check(s, p, q) == true)
{
return "Impossible";
}
char []tempArray = s.ToCharArray();
Array.Sort(tempArray);
String str = new String(tempArray);
int a = nx * ny, b = 0, i = 0, j = 0;
if (a == p && b == q)
{
return str;
}
while (1 > 0)
{
// Find index of 'x' to swap with 'y'.
for (i = 0; i < str.Length - 1; ++i)
{
if (str[i] == 'x' &&
str[i + 1] == 'y')
break;
}
for (j = i; j < str.Length - 1; j++)
{
if (str[j] == 'x' &&
str[j + 1] == 'y')
{
StringBuilder sb = new StringBuilder(str);
sb.Remove(j,1);
sb.Insert(j, str[j + 1]);
sb.Remove(j+1,1);
sb.Insert(j + 1, str[j]);
str = sb.ToString();
/* char ch[] = str.toCharArray();
char temp = ch[j+1];
ch[j+1] = ch[j];
ch[j] = temp;*/
a--; // 'xy' decrement by 1
b++; // 'yx' increment by 1
// check if count of 'xy' and
// 'yx' becomes equal to p
// and q respectively.
if (a == p && b == q)
{
return str;
}
}
}
}
}
// Driver Code
public static void Main (String[] args)
{
String s = "yxxyx";
int p = 3, q = 3;
Console.WriteLine(smallestPermutation(s, p, q));
}
}
// This code has been contributed by 29AjayKumar
Javascript
输出:
xyxyx
时间复杂度: O(N 2 )
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。