【问题标题】:How to find a sequence of letter in a given string?如何在给定的字符串中找到一个字母序列?
【发布时间】:2020-08-19 19:13:17
【问题描述】:

我的字符串是"AAABBAABABB", 我想得到结果

A = 3
B = 2
A = 2
B = 1 
A = 1
B = 2

我尝试过使用

for (int i = 0; i < n - 1; i++) {
    if (msg[i] == msg[i + 1]) {
        if(msg[i]==A)
            a++;
        else
            b++;
    }
}

我试过了,它对我不起作用。而且我不知道是否有任何其他方法可以找到它。请帮帮我。

【问题讨论】:

    标签: c++ algorithm c++11 c++14 c++17


    【解决方案1】:

    通过以下方式遍历数组:

    1. 如果 i = 0,我们可以将变量设置为第 0 个字符,计数器加 1。
    2. 如果第i个字符等于前一个字符,我们可以增加计数器。

    3. 如果第 i 个字符不等于第 (i-1) 个字符,我们可以打印该字符,计数器并开始计算新字符。

    试试下面的sn-p:

    char ch = msg[0];
    int cnt = 1;
    
    for (int i = 1; i < n; i ++){
       if(msg[i] != msg[i-1]){
          cout<<ch<<" "<<cnt<<endl;
          cnt = 1;
          ch = msg[i];
       }
       else {
          cnt++;
       }
    }
    
    cout<<ch<<" "<<cnt<<endl;
    

    【讨论】:

    • 通常,当您访问数组中的一项时,您会确保它确实拥有该项。如果 msg 实际上为空,char ch = msg[0]; 超出范围。
    【解决方案2】:

    您可以使用std::vector&lt;std::pair&lt;char, std::size_t&gt;&gt; 来存储出现的字符。

    最终,你会得到类似的东西:

    #include <iostream>
    #include <utility>
    #include <vector>
    #include <string>
    
    int main() {
        std::vector<std::pair<char, std::size_t>> occurrences;
        std::string str{ "AAABBAABABB" };
    
        for (auto const c : str) {
            if (!occurrences.empty() && occurrences.back().first == c) {
                occurrences.back().second++;
            } else {
                occurrences.emplace_back(c, 1);
            }
        }
    
        for (auto const& it : occurrences) {
            std::cout << it.first << " " << it.second << std::endl;
        }
    
        return 0;
    }
    

    它会输出:

    A 3
    B 2
    A 2
    B 1
    A 1
    B 2
    

    Demo

    【讨论】:

      【解决方案3】:

      这与游程编码非常相似。我认为我能想到的最简单的方法(更少的代码行)是这样的:

      void runLength(const char* msg) {
          const char *p = msg;
          while (p && *p) {
              const char *start = p++;  // start of a run
              while (*p == *start) p++;  // move p to next run (different run)
              std::cout << *start << " = " << (p - start) << std::endl;
          }
      }
      

      请注意:

      1. 这个函数不需要事先知道输入字符串的长度,它会停在字符串的末尾,'\0'。
      2. 它也适用于空字符串和 NULL。这两个工作:runLength("");runLength(nullptr); 我还不能评论,如果你仔细看,mahbubcseju 的代码对空味精不起作用。

      【讨论】:

      • 这是最简单的方法!
      【解决方案4】:

      使用 std,您可以这样做:

      void print_sequence(const std::string& s)
      {
          auto it = s.begin();    
          while (it != s.end()) {
              auto next = std::adjacent_find(it, s.end(), std::not_equal_to<>{});
              next = next == s.end() ? s.end() : next + 1;
      
              std::cout << *it << " = " << std::distance(it, next) << std::endl;
              it = next;
          }
      }
      

      Demo

      【讨论】:

        【解决方案5】:

        欢迎来到stackoverflow。哦,算法问题?我将添加一个递归示例:

        #include <iostream>
        
        void countingThing( const std::string &input, size_t index = 1, size_t count = 1 ) {
           if( input.size() == 0 ) return;
        
           if( input[index] != input[index - 1] ) {
              std::cout << input[index - 1] << " = " << count << std::endl;
              count = 0;
           }
        
           if( index < input.size() ) return countingThing( input, index + 1, count + 1 );
        }
        
        int main() {
           countingThing( "AAABBAABABB" );
           return 0;
        }
        

        为了帮助制定算法并弄清楚在代码中要写什么,我建议几个步骤:

        首先,以多种方式写出您的问题,它期望什么样的输入以及您希望输出的结果如何。

        其次,尝试在纸上解决它,逻辑将如何运作 - 一个很好的提示是尝试了解您将如何解决它。你的大脑是一个很好的问题解决者,如果你能倾听它的作用,你就可以把它变成代码(虽然它并不总是最有效的)。

        第三,在纸上解决问题,手动按照您的步骤操作,看看您的解决方案是否符合您的预期。然后你可以将解决方案翻译成代码,确切地知道你需要写什么。

        【讨论】:

          猜你喜欢
          • 2014-11-26
          • 2014-08-05
          • 2017-03-10
          • 2012-02-29
          • 1970-01-01
          • 1970-01-01
          • 2016-03-22
          • 1970-01-01
          • 2022-11-10
          相关资源
          最近更新 更多