【问题标题】:How can i check if all charterers in a word are existing into the other one?我如何检查一个单词中的所有承租人是否都存在于另一个中?
【发布时间】:2020-12-27 18:00:21
【问题描述】:

我正在尝试编写一个代码,从用户那里获取两个单词并将它们存储为数组,并检查第一个单词的 all 字符是否已经存在于第二个单词中,如果是,则只输出是的,如果 NO 则输出 NO。 示例:

输入:

输入第一个单词:

福斯

输入第二个字:

堆栈溢出

输出:

是的

我的代码甚至不接近我真正想要的,但我会把它记下来,也许它会解释我想要做什么。

#include <iostream>
#include <string>
using namespace std;
int main() {
    
    char N1[7];
    char N2[10];
    cout <<  ("ENTER THE FIRST WORD : ") ;
    cin >> N1;
    cout <<("ENTER THE SECOND WORD : ");
    cin >> N2;
    for (int i = 0; i <4; i++)
    {
        for (int j = 0; j < 7; j++)
        {
            if (N1[i] = N2[j]) 
            {
                cout << ("YES") << endl;
            }
            else
            {
                cout << ("NO") << endl;
            }

        }

    }
    return 0;
}

提前致谢。

【问题讨论】:

  • 提示:unordered_map&lt;string,int&gt;。另外,避免using namespace std;
  • N1[i] = N2[j] 是赋值,而不是比较。此外,C++ 有std::string,不需要笨拙的字符数组。此外,std::string 具有 std::string::find,这使您的任务更轻松。此外,您应该真正将可以命名为函数的进程分开,在这种情况下类似于bool contains_letters(const string&amp; a, const string&amp; b);
  • 您缺少的关键概念是您无法立即判断您的条件是否属实。您只能在循环结束时知道第一个字符串中的每个项目是否都在第二个字符串中。每当您遇到“全部”问题时,您都可以使用布尔变量。将变量设置为 true,然后编写一个循环来测试每个项目。如果 any 失败,则将布尔变量设置为 false。换句话说,假设您的条件为真,然后先寻找一个反例。

标签: c++ arrays substring


【解决方案1】:

我尝试使用简单的搜索和 1 个布尔变量来跟踪它是否找到。

#include <iostream>
#include <string>
using namespace std;



int main()
{
string s1  = "fosq";
string s2 = "stackoverflow"; 
  bool isfound;
  for(int i=0;i<s1.length();i++)
  {
    isfound=false;
    for(int j=0;j<s2.length();j++)
    {
      if(s1[i]==s2[j])
      {
        isfound=true;
        break;
      }
    } 
      if(!isfound)
      {
        cout<<"Not found";
        break;
      }
  }
  if(isfound)
  {
    cout<<"Yes\n";
  }
}

我正在做的是循环遍历两个字符串,比较每个元素。如果我们找不到任何元素,我只是在那里中断搜索,如果我们找到所有元素,我们就简单地输出 Yes。

【讨论】:

  • 我手动设置了s1s2的值,你可以通过cin请求输入
【解决方案2】:

我不是 c++ 专家,但您似乎想检查 all 字符是否在第二个字符串中 - 因此您最好将布尔值添加到代码中:

for (int i = 0; i <4; i++) {
    bool charFound = false;
    for (int j = 0; j < 7; j++) {
        charFound = charFound || (N1[i] == N2[j]); // use OR operator so if found one match it is will stay true
    }
    if (!charFound) {
        cout << ("NO") << endl;
        return; // one char is missing - print no and exit
    }
}
cout << ("YES") << endl; // if got here you found all char - YES

还要注意您的代码 (N1[i] == N2[j]) 中的 ==,而不是 Aziuth 的 cmets 中的 =

【讨论】:

    【解决方案3】:

    我认为最有效的方法是在计算匹配之前对两个数组进行排序:

    #include <iostream>
    #include <algorithm>
    #include <array>
    
    template <typename T1, typename T2>
    bool match(T1& arr1, T2& arr2) {
        for (char c2: arr2) {
            for (char c1: arr1) {
                if (!c1 || !c2) continue;
                if (c1 == c2) break;
                if (c1 > c2) return false;
            }
        }
        return true;
    }
    int main() {
        std::array<char, 14> arr1 {"stackoverflow"};
        std::array<char, 4> arr2 {"fos"};
    
        std::sort(arr1.begin(), arr1.end());
        std::sort(arr2.begin(), arr2.end());
        
        if (match(arr1, arr2)) {
            std::cout << "YES" << std::endl;
        } else {
            std::cout << "NO" << std::endl;
        }
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-04-21
      • 2021-11-08
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      • 2012-12-12
      • 1970-01-01
      相关资源
      最近更新 更多