字符串展开

给定一个字符串,字符串包含数字,大小写字母与括号,(包括大括号,中括号和小括号)括号可以嵌套,即括号里边可以出现数字和括号,按照如下的跪着对字符串进行展开,不需要考虑括号不成对匹配的问题,用例保证括号匹配,同时保证每个数字后面都有括号,不用考虑数字后边没有括号的情况,即  2a2(b)这种情况不用考虑。

  1)数字表示括号里的字符串重复的次数,展开后的字符串不包括括号。

  2)字符串展开是逆序展开

输入描述:

      输入一个长度小于100的字符串

输出描述:

   输出展开后的字符串

示例1:输入输出示例仅供调试,后台判断数据一般不包含示例

       输入    abc3(A)

       输出   AAAcba

解析:  个人觉得题目中有些地方没解释清楚,如果再增加一个示例就好了,比如如果出现了括号嵌套的情况,首层括号要重复的内容是否要包含嵌套括号的内容,还有就是是否要包括首层括号里边的数字,还有就是括号里边的内容是否要颠倒过来

    个人理解的是,嵌套时候,首层括号重复时包含里边括号的内容,不含数字(即重复首层括号里除括号数字外所有字符),且括号内输出不倒序

解题思路:设计两个队列,正着查输入字符串,记录每个出现( { [ 的位置,加入队列

                                          倒着查输入字符串,记录每个出现 )} ]的位置,加入队列,

                这样保证 出队列的时候都是对应配对括号的位置,如果最后一个括号)} ] 后还有字符串,倒过来放到输出最前

                                                                                                如果第一个括号 ( [ { 前还有字符串,倒过来放到最后

               每次取出配对括号里的内容,构造一个filter函数,滤除所有嵌套的括号与数字,给与队列输出的左括号的位置,往前取一位求出要重复的次数。输出字符串加上  filter后的字符串repeat。    队列取空后,加上之前 第一个左括号前倒置的字符串,输出。

时间紧张,语句难免有语病,或者歧义的地方,回头有空自己再整理下

代码如下:

#include <iostream>
#include <string>
#include <string.h>
#include <queue>
#include <algorithm>

using namespace std;

string filter(string str){
    int len = str.length();
    string result="";
    for(int i=0;i<len;i++){
        if((str[i]>='A'&&str[i]<='Z') ||(str[i]>='a'&&str[i]<='z')){
            result += str[i];
        }
    }
    return result;
}

bool isLeft(char ch){
    if(ch=='('||ch=='['||ch=='{'){
        return true;
    }
    else return false;
}

bool isRight(char ch){
    if(ch==')'||ch==']'||ch=='}'){
        return true;
    }
    else return false;
}


int main(){
 string inStr ;
 cin>>inStr;
 int inEnd = inStr.length()-1;     //store the length of inStr;
 int beg = 0;
 int end = 0;     
 queue<int> iter_end;
 queue<int> iter_beg;
 
 for(int i=0;i<=inEnd;i++){    //push every ( [ { in the queue from left to right
     if(isLeft(inStr[i]))  iter_beg.push(i);
 }
 
 for(int i=inEnd;i>0;i--){   //push every ) ] } in the queue from right to left
     if(isRight(inStr[i]))  iter_end.push(i);
 }
 
 int num = iter_beg.size();     //count all the brace in the string
 string outStr = "";    //store the output string
 string outEndStr = "";  //store the last part of the out string
 
 
 if(num ==0){
     reverse(inStr.begin(),inStr.end());
     cout<<inStr<<endl;
     return 0;
 }
 
 for(int i=1;i<=num;i++){
     beg = iter_beg.front();
     end = iter_end.front();
     string temp = "";    //store the string add to outStr;
     
     if(i==1){   //if the first brace
     if(end!=inEnd){
          temp = inStr.substr(end+1,inEnd-end);
          reverse(temp.begin(),temp.end());
          outStr += temp;
          temp = "";
     }
     if(beg !=1){
         temp = inStr.substr(0,beg-1);
         reverse(temp.begin(),temp.end());
         outEndStr = temp;
         temp = "";
     }
 
     }
     int repeat = inStr[beg-1]-'0'; //record the repeat times of a string in brace
     temp = filter(inStr.substr(beg+1,end-beg-1));
     for(int j=0;j<repeat;j++) outStr += temp;
    iter_beg.pop();
    iter_end.pop();
     
 }
 outStr += outEndStr;
 cout<<outStr<<endl;

}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值