📌  相关文章
📜  用于检查给定字符串是否可以由其他两个字符串或其排列组成的 C++ 程序

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

用于检查给定字符串是否可以由其他两个字符串或其排列组成的 C++ 程序

给定一个字符串str和一个字符串数组arr[] ,任务是检查给定的字符串是否可以由数组中的任何字符串对或其排列组成。

例子:

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if str can be
// generated from any permutation of the
// two strings selected from the given vector
bool isPossible(vector v, string str)
{
  
    // Sort the given string
    sort(str.begin(), str.end());
  
    // Select two strings at a time from given vector
    for (int i = 0; i < v.size() - 1; i++) {
        for (int j = i + 1; j < v.size(); j++) {
  
            // Get the concatenated string
            string temp = v[i] + v[j];
  
            // Sort the resultant string
            sort(temp.begin(), temp.end());
  
            // If the resultant string is equal
            // to the given string str
            if (temp.compare(str) == 0) {
                return true;
            }
        }
    }
  
    // No valid pair found
    return false;
}
  
// Driver code
int main()
{
    string str = "amazon";
    vector v{ "fds", "oxq", "zoa", "epw", "amn" };
  
    if (isPossible(v, str))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


C++
// C++ implementation of the approach
#include 
using namespace std;
#define MAX 26
  
// Function to sort the given string
// using counting sort
void countingsort(string& s)
{
    // Array to store the count of each character
    int count[MAX] = { 0 };
    for (int i = 0; i < s.length(); i++) {
        count[s[i] - 'a']++;
    }
    int index = 0;
  
    // Insert characters in the string
    // in increasing order
    for (int i = 0; i < MAX; i++) {
        int j = 0;
        while (j < count[i]) {
            s[index++] = i + 'a';
            j++;
        }
    }
}
  
// Function that returns true if str can be
// generated from any permutation of the
// two strings selected from the given vector
bool isPossible(vector v, string str)
{
  
    // Sort the given string
    countingsort(str);
  
    // Select two strings at a time from given vector
    for (int i = 0; i < v.size() - 1; i++) {
        for (int j = i + 1; j < v.size(); j++) {
  
            // Get the concatenated string
            string temp = v[i] + v[j];
  
            // Sort the resultant string
            countingsort(temp);
  
            // If the resultant string is equal
            // to the given string str
            if (temp.compare(str) == 0) {
                return true;
            }
        }
    }
  
    // No valid pair found
    return false;
}
  
// Driver code
int main()
{
    string str = "amazon";
    vector v{ "fds", "oxq", "zoa", "epw", "amn" };
  
    if (isPossible(v, str))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


方法2:可以使用计数排序来减少上述方法的运行时间。计数排序使用一个表来存储每个字符的计数。我们有 26 个字母,因此我们制作了一个大小为 26 的数组来存储字符串中每个字符的计数。然后将字符按升序排列,得到排序后的字符串。
下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
#define MAX 26
  
// Function to sort the given string
// using counting sort
void countingsort(string& s)
{
    // Array to store the count of each character
    int count[MAX] = { 0 };
    for (int i = 0; i < s.length(); i++) {
        count[s[i] - 'a']++;
    }
    int index = 0;
  
    // Insert characters in the string
    // in increasing order
    for (int i = 0; i < MAX; i++) {
        int j = 0;
        while (j < count[i]) {
            s[index++] = i + 'a';
            j++;
        }
    }
}
  
// Function that returns true if str can be
// generated from any permutation of the
// two strings selected from the given vector
bool isPossible(vector v, string str)
{
  
    // Sort the given string
    countingsort(str);
  
    // Select two strings at a time from given vector
    for (int i = 0; i < v.size() - 1; i++) {
        for (int j = i + 1; j < v.size(); j++) {
  
            // Get the concatenated string
            string temp = v[i] + v[j];
  
            // Sort the resultant string
            countingsort(temp);
  
            // If the resultant string is equal
            // to the given string str
            if (temp.compare(str) == 0) {
                return true;
            }
        }
    }
  
    // No valid pair found
    return false;
}
  
// Driver code
int main()
{
    string str = "amazon";
    vector v{ "fds", "oxq", "zoa", "epw", "amn" };
  
    if (isPossible(v, str))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


请参阅有关检查给定字符串是否可以由其他两个字符串或其排列组成的完整文章以获取更多详细信息!