【问题标题】:One char from the other bool function另一个布尔函数的一个字符
【发布时间】:2017-01-30 12:05:22
【问题描述】:

我想要一个函数,当且仅当可以通过简单地删除某些字母(例如g("ERT", "EAARYT")=true 和 g("ERT","ABCT")=false。

我对这段代码的想法如下:

bool g(char* s, char* t) {
    for (int i=0; s[i]!=0;i++) {
        for (int j=i; t[j]!=0; j++) {
            if (s[i]==t[j]) {
                return true;
            }
        }
    }
    return false;
}

显然它不起作用,因为它只检查第一个字母是否存在,然后立即返回 true。 我应该如何改变它?

我更喜欢使用嵌套循环/if 构造,这应该是可行的。

【问题讨论】:

  • 那就不能正常工作了。
  • 你考虑过不立即返回true吗?
  • 另外,使用一个循环。将i 分配给j 是错误的。
  • 第一个循环结束之前?还是在哪里?
  • @LogicStuff 但是如何遍历 char 呢?

标签: c++ loops if-statement for-loop


【解决方案1】:

当且仅当 char *s 可以通过简单地删除某些字母从 char *t 获得时返回 true。这意味着两件事:

  • [A] t 应该包含s 的所有字符。因此,我们需要存储字符数。
  • [B] t 应包含s 的所有字符,t 应具有与s 相同的公共字符。

解决方案 A:t 中的字符顺序无关紧要。重要的是s 中的所有字符都应该出现在t 中。

g("ERRT", "ERT") = false

g("ERRR", "ERRT") = false

g("ERRR", "RRRE") = true

bool g(char* s, char* t) 
{
    int hash[256] = {0};
    int i;
    for(i = 0; t[i]; ++i) 
    {
        hash[t[i]] += 1;
    }

    for(i = 0; s[i]; ++i) 
    {
        if(hash[s[i]] <= 0)
        {
            return false;
        }
        else
        {
            hash[s[i]] -= 1;
        }
    }

    return true;
}

解决方案 B:t 中的字符顺序确实很重要。 s 中的所有字符也应该出现在 t 中。

g("ERRT", "RERT") = false

g("ERRR", "RRER") = false

g("ERRR", "RRRE") = false

g("ERTR", "RERGRTBR") = true

bool g(char* s, char* t) 
{
    if(strcmp(LCS(s, t), s) == 0)
    {
        return true;
    }

    return false;
}    

在哪里,LCS(s, t) =Longest Common Subsequence

【讨论】:

    【解决方案2】:

    如果没有找到后者,你可以返回false,否则成功:

    bool g(const char* s, const char* t) {
      for (int i = 0; s[i] != 0; i++) {
        bool found = false;
        for (int j = i; t[j] != 0; j++) {
          if (s[i] == t[j]) {
            found = true;
          }
        }
        if (!found) {
          return false;
        }
      }
      return true;
    }
    

    另外,考虑使用strspn 来计算匹配字符

    bool g(const char* s, const char* t){
        return strspn(s, t) == strlen(s);
    }
    

    【讨论】:

    • 很有趣,但strcpn 会像这个问题一样使用非连续匹配吗?
    【解决方案3】:

    你可以这样做-

    bool g(char* s, char* t){
        int sLen=strlen(s);
        int tLen=strlen(t);
        if(sLen > tLen)
        {
            return false;
        }
    
        int j=0;
        for(int i=0;i<tLen;i++)
        {
            if(s[j] == t[i])
            {
                j++;
            }
            if(j == sLen)
            {
                return true;
            }
        }
        return false;
    }
    

    【讨论】:

    • 这里假设s 和t 是按它们里面的字符排序的?
    【解决方案4】:

    试试这个:

        int c = 0;
        int len = strlen(s); // Find the length of string
        for(i=0; s[i]!=0;i++)
        {
                for(j=0; t[j]!=0; j++)
                {
                        if(s[i]==t[j])
                        {
                                c++; // count the character, If count is equal to length then true.
                        }
                }
        }
        if (len == c) 
                return true;
        else
                return false;
    

    【讨论】:

    • if(len>=c) return true;
    • @user1438832 计数等于长度,不多也不少。
    • @user1438832 好的,没问题。
    • 但是如果 s 是 "XXXX" 而 t 是 "X" 你的代码会返回 true。
    • 是的,因为第一个字符串每个字符都与第二个字符串进行比较。
    【解决方案5】:

    我将只使用一个循环来编写函数,并将第二个循环替换为标准 C 函数strchr。例如

    #include <iostream>
    #include <iomanip>
    #include <cstring>
    
    bool g( const char *s, const char *t )
    {
        size_t m = std::strlen( s );
        size_t n = std::strlen( t );
    
        const char *p;
    
        while ( ( m != 0 ) and not ( n < m ) and ( p = std::strchr( t, s[0] ) ) )
        {
            ++p;
            n -= p - t;
            t = p;
            --m;
            ++s; 
        }
    
        return m == 0;
    }
    
    int main() 
    {
        std::cout << "g( \"ERT\", \"EAARYT\" ) = " 
                  << std::boolalpha << g( "ERT", "EAARYT" ) << std::endl;       
    
        std::cout << "g( \"ERT\", \"ABCT\" ) = " 
                  << std::boolalpha << g( "ERT", "ABCT" ) << std::endl;     
    }
    

    程序输出是

    g( "ERT", "EAARYT" ) = true
    g( "ERT", "ABCT" ) = false
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-05
      • 2014-07-13
      • 2016-07-20
      • 2021-02-16
      • 2013-03-18
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多