给出一个字符X和一个字符串的长度N Y和任务是通过附加字符的左侧和X的右端找到方法来转换X到Y的数量。请注意,如果左右附加的序列不同,或者如果序列相同,则任何两种方式都被认为是不同的,则附加的字符不同,即左附加后跟右附加与右附加后跟左不同附加。由于答案可以大写,因此最终答案 MOD (10 9 + 7)。
例子:
Input: X = ‘a’, Y = “xxay”
Output: 3
All possible ways are:
- Left append ‘x’ (“xa”), left append ‘x’ (“xxa”), right append y(“xxay”).
- Left append ‘x’ (“xa”), right append y(“xay”), left append ‘x’ (“xxay”).
- Right append y(“ay”), left append ‘x’ (“xay”), left append ‘x’ (“xxay”).
Input: X = ‘a’, Y = “cd”
Output: 0
方法 1:解决此问题的一种方法是使用动态规划。
- 初始化一个变量 ans = 0,mod = 1000000007。
- 对于所有索引 ‘i’ 使得 Y[i] = X,将答案更新为 ans = (ans + dp[i][i])%mod。
这里, dp[l][r]是从子串Y[l…r] 生成Y的方法数。
递推关系为:
dp[l][r] = (dp[l][r + 1] + dp[l – 1][r]) % mod
这种方法的时间复杂度为 O(N 2 )。
方法二:
- 初始化一个变量 ans = 0,mod = 1000000007。
- 对于所有索引i使得Y[i] = X ,将答案更新为ans = (ans + F(i)) % mod其中F(i) = (((N – 1)!) / (i! * (N – i – 1)!)) % mod 。
上述公式有效的原因:只需尝试找到问题的答案,找出 (p 个 L) 和 (q 个 R) 的排列数,其中 L 和 R 分别是左追加和右追加操作。
答案是(p + q)! / (p! * q!) 。对于每个有效的i ,只需找到i Ls 和N – i – 1 Rs 的排列数。
这种方法的时间复杂度为O(N) 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
const int MOD = 1000000007;
// Function to find the modular-inverse
long long modInv(long long a, long long p = MOD - 2)
{
long long s = 1;
// While power > 1
while (p != 1) {
// Updating s and a
if (p % 2)
s = (s * a) % MOD;
a = (a * a) % MOD;
// Updating power
p /= 2;
}
// Return the final answer
return (a * s) % MOD;
}
// Function to return the count of ways
long long findCnt(char x, string y)
{
// To store the final answer
long long ans = 0;
// To store pre-computed factorials
long long fact[y.size() + 1] = { 1 };
// Computing factorials
for (long long i = 1; i <= y.size(); i++)
fact[i] = (fact[i - 1] * i) % MOD;
// Loop to find the occurrences of x
// and update the ans
for (long long i = 0; i < y.size(); i++) {
if (y[i] == x) {
ans += (modInv(fact[i])
* modInv(fact[y.size() - i - 1]))
% MOD;
ans %= MOD;
}
}
// Multiplying the answer by (n - 1)!
ans *= fact[(y.size() - 1)];
ans %= MOD;
// Return the final answer
return ans;
}
// Driver code
int main()
{
char x = 'a';
string y = "xxayy";
cout << findCnt(x, y);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
final static int MOD = 1000000007;
// Function to find the modular-inverse
static long modInv(long a)
{
long p = MOD - 2;
long s = 1;
// While power > 1
while (p != 1)
{
// Updating s and a
if (p % 2 == 1)
s = (s * a) % MOD;
a = (a * a) % MOD;
// Updating power
p /= 2;
}
// Return the final answer
return (a * s) % MOD;
}
// Function to return the count of ways
static long findCnt(char x, String y)
{
// To store the final answer
long ans = 0;
// To store pre-computed factorials
long fact[] = new long[y.length() + 1];
for(int i = 0; i < y.length() + 1; i++)
fact[i] = 1;
// Computing factorials
for (int i = 1; i <= y.length(); i++)
fact[i] = (fact[i - 1] * i) % MOD;
// Loop to find the occurrences of x
// and update the ans
for (int i = 0; i < y.length(); i++)
{
if (y.charAt(i) == x)
{
ans += (modInv(fact[i])
* modInv(fact[y.length() - i - 1])) % MOD;
ans %= MOD;
}
}
// Multiplying the answer by (n - 1)!
ans *= fact[(y.length() - 1)];
ans %= MOD;
// Return the final answer
return ans;
}
// Driver code
public static void main (String[] args)
{
char x = 'a';
String y = "xxayy";
System.out.println(findCnt(x, y));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
MOD = 1000000007;
# Function to find the modular-inverse
def modInv(a, p = MOD - 2) :
s = 1;
# While power > 1
while (p != 1) :
# Updating s and a
if (p % 2) :
s = (s * a) % MOD;
a = (a * a) % MOD;
# Updating power
p //= 2;
# Return the final answer
return (a * s) % MOD;
# Function to return the count of ways
def findCnt(x, y) :
# To store the final answer
ans = 0;
# To store pre-computed factorials
fact = [1]*(len(y) + 1) ;
# Computing factorials
for i in range(1,len(y)) :
fact[i] = (fact[i - 1] * i) % MOD;
# Loop to find the occurrences of x
# and update the ans
for i in range(len(y)) :
if (y[i] == x) :
ans += (modInv(fact[i]) *
modInv(fact[len(y)- i - 1])) % MOD;
ans %= MOD;
# Multiplying the answer by (n - 1)!
ans *= fact[(len(y) - 1)];
ans %= MOD;
# Return the final answer
return ans;
# Driver code
if __name__ == "__main__" :
x = 'a';
y = "xxayy";
print(findCnt(x, y));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
static int MOD = 1000000007;
// Function to find the modular-inverse
static long modInv(long a)
{
long p = MOD - 2;
long s = 1;
// While power > 1
while (p != 1)
{
// Updating s and a
if (p % 2 == 1)
s = (s * a) % MOD;
a = (a * a) % MOD;
// Updating power
p /= 2;
}
// Return the final answer
return (a * s) % MOD;
}
// Function to return the count of ways
static long findCnt(char x, String y)
{
// To store the final answer
long ans = 0;
// To store pre-computed factorials
long []fact = new long[y.Length + 1];
for(int i = 0; i < y.Length + 1; i++)
fact[i] = 1;
// Computing factorials
for (int i = 1; i <= y.Length; i++)
fact[i] = (fact[i - 1] * i) % MOD;
// Loop to find the occurrences of x
// and update the ans
for (int i = 0; i < y.Length; i++)
{
if (y[i] == x)
{
ans += (modInv(fact[i])
* modInv(fact[y.Length - i - 1])) % MOD;
ans %= MOD;
}
}
// Multiplying the answer by (n - 1)!
ans *= fact[(y.Length - 1)];
ans %= MOD;
// Return the final answer
return ans;
}
// Driver code
public static void Main ()
{
char x = 'a';
string y = "xxayy";
Console.WriteLine(findCnt(x, y));
}
}
// This code is contributed by AnkitRai01
Javascript
输出:
6
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。