检查你是否可以用word1的字母组成word2

时间:2017-10-20 20:45:47

标签: c++

任务是输入两个由大写拉丁字母组成的单词,这些单词是> 1和<250个字符,并且程序输出是否第二个单词可以由第一个单词的字母组成。

我只通过了11/12测试。在询问了3个人的帮助并使用他们的代码后,我开始得到4/12 ...这就是我在这里的原因。

我的代码:

#include <iostream>
using namespace std;

int main()
{
    string word1, word2; // inputted words
    cin >> word1 >> word2;
    if (word2.size() > word1.size())
    {
        return 0;
    }
    for (int i = 0; i < word1.size(); i++) // safe proofing
    {
        if (islower(word1[i]) || islower(word2[i]) || word1.size() < 1
                || word1.size() > 250 || word2.size() < 1 || word2.size() > 250)
        {
            return 0;
        }
    }
    int sk1 = 0, sk2 = 0; // times a letter appears in the respective words
    int a = 0; // variable outside of for loop for the end if statement to work
    for (a; a < word2.size(); a++) //checks how many times each letter of the 2nd 
                                   //word appears in both words
    {
        for (int i = 0; i < word1.size(); i++) //checks how many times the letter 
                                               //appears in the 1st word
        {
            if (word2[a] == word1[i])
            {
                sk1++; // counts the times it appears
            }
        }
        for (int i = 0; i < word2.size(); i++) //checks how many times the letter 
                                               //appears in the 2nd word
        {
            if (word2[a] == word2[i])
            {
                sk2++; // counts the times it appears
            }
        }
        if (sk1 < sk2) // if the 1st word has less of the letter than needed, 
                       //it outputs that you can't make the 2nd word
        {
            cout << "NO";
            break;
        }
        sk1 = 0;
        sk2 = 0;
    }
    if (a == word2.size()) //if it goes through all the letters and there are enough 
                           //of them to make the 2nd word, it says that it can be made.
    {
        cout << "YES";
    }
}

3 个答案:

答案 0 :(得分:2)

  1. 首先,你拿一个数组a[26]vector
  2. 用0&#39;初始化它。
  3. 然后为第一个单词中的每个字母增加相应的位置。 a[first[i]-'A']++;
  4. 对于第二个单词,请执行相同操作但此时间减少1. a[second[i]-'A']--;
  5. 最后检查阵列位置是否为<0。如果是,那么&#39;不,你不能说出第二个字'&#39;别的you can
  6. 如果超过26个字符,您可以使用地图。 map<char,int> m并重复同样的事情。这会有所帮助。

    您使用的算法很复杂。先想想你将如何手动完成。这有时会有所帮助。

    C ++ 11解决方案

    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
        string word1, word2; // inputted words
        cin >> word1 >> word2;
        vector<int> a(26,0);
        for(auto x:word1)a[x-'A']++;
        for(auto x:word2)a[x-'A']--;
        for(auto x:a)if(x<0){cout<<"NO\n"; return 0;}
        cout<<"YES\n";
        return 0;
    }
    

答案 1 :(得分:1)

如果你想知道是否可以从第一个单词的字母构造第二个单词而不管字母的出现次数,那么下面的代码正是使用 C ++ set

#include <bits/stdc++.h>
using namespace std;


int main()
{
    string word1, word2;
    set<char> set1;
    set<char> set2;
    string output;

    while(cin>> word1 >> word2)
    {
        set1 = set<char>(word1.begin(), word1.end());
        set2 = set<char>(word2.begin(), word2.end());

        output.clear();
        set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), back_inserter(output));

        set<char>(output.begin(), output.end()) == set2 ? cout << "Yes\n" : cout << "No\n";
    }

    return 0;
}

答案 2 :(得分:0)

您可以输入两个字符串并将它们转换为大写或小写,然后使用两个for循环检查第一个字符串中是否包含第二个字符的所有字符。

每当发现一个字符增加变量nFound并立即中断嵌入式循环以检查下一个字符。

最后,如果nFound与input2的大小一样大,则返回true,否则返回false。

#include <iostream>
#include <string>


bool Str2FromStr1(std::string, std::string);

int main(int argc, const char *argv[]){

    std::string strName1, strName2;

    std::cout << "Name1: ";
    std::getline(std::cin, strName1);

    std::cin.ignore(1, '\n');
    std::cout << "Name2: ";
    std::getline(std::cin, strName2);

    if(strName2 > strName1){
        std::cout << "strName2 bust be longer than or equal to strName1!" << std::endl;
        return 1;
    }

    std::cout << Str2FromStr1(strName1, strName2) << std::endl;

    std::cout <<  std::endl;
    return 0;
}

bool Str2FromStr1(std::string str1, std::string str2){

    const int size1 = str1.length();
    const int size2 = str2.length();

    for(int i(0); i < size1; i++)
        str1[i] = toupper(str1[i]);
    for(int i(0); i < size2; i++)
        str2[i] = toupper(str2[i]);

    int nFound = 0;
    for(int i(0); i < size2; i++){
        for(int j (0); j < size1; j++){
            if(str2[i] == str1[j]){
                nFound++;
                break;
            }
        }
    }

    return ((nFound == size2) ? true : false);
}