【问题标题】:Remove adjacent Duplicates from a String which have count greater than or equal to burst Length从字符串中删除计数大于或等于突发长度的相邻重复项
【发布时间】:2020-08-31 18:53:07
【问题描述】:

给定一个包含重复字符和突发长度的字符串,输出字符串使得字符串中相同相邻字符的计数小于突发长度。

输入:abbccccdd,burstLen = 3
正确的输出:abbdd
我的输出:abbd


输入:abbcccdeaffff,burstLen = 3
正确的输出:abbdea
我的输出:abbea

//Radhe krishna ki jytoi alokik
#include <bits/stdc++.h>
using namespace std;

string solve(string s, int burstLen)
{
    stack<pair<char, int>> ms;
    
    for (int i = 0; i < s.size(); i++)
    {
        if (!ms.empty() && ms.top().first == s[i])
        {
            int count = ms.top().second;
            ms.push({s[i], count + 1});
        }
        
        else
        {
            if(ms.empty() == true  ||  ms.top().first != s[i])
            {
                if(!ms.empty() && ms.top().second >= burstLen)
                {
                    int count = ms.top().second;
                    
                    while(!ms.empty() && count--)
                        ms.pop();
                    //(UPDATE)
                     ms.push({s[i], 1});
                }
                
                else
                    ms.push({s[i], 1});
            }
        }

    }
    
    if(!ms.empty() and ms.top().second >= burstLen)
    {
        int count = ms.top().second;
        while(!ms.empty() && count--)
            ms.pop();
    }

    string ans = "";
    while (!ms.empty())
    {
        ans += ms.top().first;
        ms.pop();
    }
    
    reverse(ans.begin(), ans.end());    
    return ans;
}


int main()
{


    
        string s;
        int burstLen;

        cin >> s;
        cin >> burstLen;

        cout << solve(s, burstLen) << "\n";
}

【问题讨论】:

  • 小心#include &lt;bits/stdc++.h&gt;using namespace std;。他们只需几行代码就可以破坏程序。
  • 我认为你过于复杂了。当您扫描字符串时,您可以计算您看到当前字符的次数。当您看到一个新字符时,如果计数小于突发长度,则将该字符添加到输出字符串中,重置计数器并开始处理新字符。
  • 您的问题似乎缺少问题部分。您在寻找代码审查吗?
  • 您的问题是什么?你的代码不符合你的要求吗?你得到编译器错误吗?请把它们包括在问题中。输出错误?请在问题中包含输入、输出和预期输出
  • 是的,有点,我用笔和纸试了一下,但在某处我缺少一些条件,是的,需要帮助来解决这个问题。

标签: c++ string algorithm data-structures stack


【解决方案1】:

至少使用容器适配器std::queue而不是std::stack会更好,因为不需要调用算法std::reverse

此外,如果堆栈中的项目包含存储频率的第二个数据成员,那么您可以只为重复字符增加此数据成员,而不是将每个重复字符放入堆栈中。

例如你程序中的这段代码sn-p

    if (!ms.empty() && ms.top().first == s[i])
    {
        int count = ms.top().second;
        ms.push({s[i], count + 1});
    }

使函数定义过于复杂和不清楚,因为相同的字符以不同的频率被压入堆栈。

不过,如果你想使用容器适配器 std::stack ,函数定义可能看起来更简单。您没有使用 std::string 类的功能。

这是一个演示程序,展示了如何使用 std::stack 的方法编写函数。

#include <iostream>
#include <string>
#include <utility>
#include <stack>
#include <iterator>
#include <algorithm>

std::string solve( const std::string &s, size_t burstLen )
{
    std::stack<std::pair<char, size_t>> stack;
    
    for ( const auto &c : s )
    {
        if ( stack.empty() || stack.top().first != c )
        {
            stack.push( { c, 1 } );
        }
        else
        {
            ++stack.top().second;
        }
    }

    std::string ans;
    
    while ( !stack.empty() )
    {
        if ( stack.top().second < burstLen )
        {
            ans.append( stack.top().second, stack.top().first );
        }
        stack.pop();
    }
    
    std::reverse( std::begin( ans ), std::end( ans ) );
    
    return ans;
}

int main()
{
    std::cout << solve( "abbccccdd", 3 ) << '\n';
    std::cout << solve( "abbcccdeaffff", 3 ) << '\n';
}

程序输出是

abbdd
abbdea

有趣的是,在删除不小于从左侧和右侧子序列中获得的突发长度的字符序列后,使用堆栈再次不小于突发长度并且您还需要删除它。

在这种情况下,您可以使用两个堆栈。

这是一个演示程序。

#include <iostream>
#include <string>
#include <utility>
#include <stack>
#include <iterator>
#include <algorithm>

std::string solve( const std::string &s, size_t burstLen )
{
    std::stack<std::pair<char, size_t>> stack_in;
    
    for ( const auto &c : s )
    {
        if ( stack_in.empty() || stack_in.top().first != c )
        {
            stack_in.push( { c, 1 } );
        }
        else
        {
            ++stack_in.top().second;
        }
    }

    std::stack<std::pair<char, size_t>> stack_out;

    while ( !stack_in.empty() )
    {
        if ( !stack_out.empty() && stack_out.top().first == stack_in.top().first )
        {
            if ( stack_out.top().second + stack_in.top().second < burstLen )
            {
                stack_out.top().second += stack_in.top().second;
            }
            else
            {
                stack_out.pop();
            }
        }
        else if ( stack_in.top().second < burstLen )
        {
            stack_out.push( stack_in.top() );
        }
        
        stack_in.pop();
    }
    
    std::string ans;
    
    while ( !stack_out.empty() )
    {
        ans.append( stack_out.top().second, stack_out.top().first );
        stack_out.pop();
    }
    
    return ans;
}


int main()
{
    std::cout << solve( "abbccccdd", 3 ) << '\n';
    std::cout << solve( "abbcccdeaffff", 3 ) << '\n';
    std::cout << solve( "aabcddeeedccbaa", 3 );
}

程序输出是

abbdd
abbdea
aabbaa

【讨论】:

    【解决方案2】:

    我试了一下,但它看起来很复杂,所以我建议使用标准库中的一些函数制作一个更简单的函数。

    例子:

    #include <algorithm>
    #include <iostream>
    #include <initializer_list>
    #include <iterator>
    
    std::string solve(const std::string& in, size_t burstlen) {
        std::string retval;
    
        for(std::string::const_iterator begin = in.cbegin(), bend;
            begin != in.end();
            begin = bend) 
        {
    
            // find the first char not equal to the current char
            bend = std::find_if_not(std::next(begin), in.end(), 
                                    [curr=*begin](char ch){ return ch==curr; });
    
            if(std::distance(begin, bend) < burstlen) {
                // length ok, append it
                retval.append(begin, bend);
            }
        }
    
        return retval;
    }
    
    int main() {
        std::initializer_list<std::string> tests{
            "abbccccdd", "abbcccdeaffff"};
        for(auto test : tests) std::cout << solve(test, 3) << '\n';
    }
    

    输出:

    abbdd
    abbdea
    

    【讨论】:

      【解决方案3】:

      我的解决方法:

      创建一个由字符和字符数组成的堆栈

      如果Stack为空或者栈顶元素不等于字符串中的当前元素

      情况1:如果栈顶元素的出现频率大于等于k,则将其存储在一个变量say count中,弹出Stack count次的元素。

      情况 2:如果 Stack 为 Empty,则只需以频率 1 将元素压入堆栈。

      在遍历整个字符串时,如果栈顶元素的频率大于 bursten,则开始从栈中移除元素(计数)次。

      现在,我们在堆栈中有遗漏的元素,开始弹出它们并将它们存储在一个字符串中并反转字符串以保持顺序。

      返回新字符串。

      更新:已解决。在这种情况下缺少一行 if(ms.empty() == true || ms.top().first != s[i]) 弹出元素后,我们还必须插入字符频率为 1 的当前元素。

      #include<iostream>
      #include<stack>
      using namespace std;
      
      string solve(string s, int burstLen)
      {
          stack<pair<char, int>> ms;
          
          for (int i = 0; i < s.size(); i++)
          {
              if (!ms.empty() && ms.top().first == s[i])
              {
                  int count = ms.top().second;
                  ms.push({s[i], count + 1});
              }
              
              else
              {
                  if(ms.empty() == true  ||  ms.top().first != s[i])
                  {
                      if(!ms.empty() && ms.top().second >= burstLen)
                      {
                          int count = ms.top().second;
                          
                          while(!ms.empty() && count--)
                              ms.pop();
                              
                          ms.push({s[i], 1});
                      }
                      
                      else
                          ms.push({s[i], 1});
                  }
              }
      
          }
          
          if(!ms.empty() and ms.top().second >= burstLen)
          {
              int count = ms.top().second;
              while(!ms.empty() && count--)
                  ms.pop();
          }
      
          string ans = "";
          while (!ms.empty())
          {
              ans += ms.top().first;
              ms.pop();
          }
          
          reverse(ans.begin(), ans.end());    
          return ans;
      }
      
      
      int main()
      {
      
      
          int t;
          cin >> t;
          
          while(t--)
          {
              string s;
              int burstLen;
              cin >> s >>burstLen;
      
              cout << solve(s, burstLen) << "\n";
          }
      
      }
      

      【讨论】:

      • 请从问题中删除解决方案的说明,并将其添加到答案中。
      • 请删除bits/stdc++ 头文件,因为它不是标准的,我们也不支持。
      • @ThomasMatthews 现在是吗?
      • @user4581301 我会尝试为此添加一个更简洁的解决方案。
      • @Anuragmishra 不,那是一个月前的事了。虽然可能是几天后。不确定。你在说什么?停止使用“bits/stdc++”头文件。
      猜你喜欢
      • 1970-01-01
      • 2018-03-05
      • 1970-01-01
      • 2021-08-04
      • 2015-08-04
      • 2012-06-26
      • 1970-01-01
      • 2019-05-12
      相关资源
      最近更新 更多