码迷,mamicode.com
首页 > 其他好文 > 详细

zuma_list(使用string类 实现)

时间:2019-03-14 13:28:04      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:++   size   span   使用   using   namespace   operation   ret   index   

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

//Solution
void del(int index, string ball, string &zuma, int len, bool flag);
int main(){
    string ball;
    int operations,index;
    int len;
    string zuma;
    getline(cin, zuma);
    cin >> operations;
    int te = operations;
    while(operations > 0){
        cin >> index;
        cin >> ball;
        zuma.erase(0,zuma.find_first_not_of(" "));
        len = zuma.size();
        if(index<len+1 && index > -1){
        zuma.insert(index, ball);
        del(index,ball,zuma,len+1, 0);//删除祖玛
        }
        len = zuma.size();
        if(len == 0)
            cout << "-" << endl;
        else
            cout << zuma << endl;
        operations --;
    }
    zuma.erase(0,zuma.find_first_not_of(" "));
    len = zuma.size();
    if(te == 0 && len != 0)
        cout << zuma << endl;
    if(te == 0 && len == 0)
        cout << "-"<< endl;
    return 0;
}

void del(int index, string ball, string &zuma,int len, bool flag){
    int left_point = index;
    int right_point = len-1>index+1?index+1:len-1;
    while(left_point>0)
        if(ball[0] == zuma[left_point-1])
            left_point--;
        else
            break;

    while(ball[0] == zuma[right_point])
        if(right_point<len-1)
            right_point ++;
        else
            break;
    if(right_point - left_point > 1)
    {
        if(right_point - left_point == 2 && ball[0] != zuma[right_point])
            return ;
        else 
            if(flag == 0 || (flag == 1 && left_point!=index))//左指针必须移动
        {
        if(ball[0] == zuma[right_point])
            zuma.erase(left_point, right_point-left_point+1);
        else
            zuma.erase(left_point, right_point-left_point);
        len = zuma.size();
        ball[0] = zuma[left_point];
        del(left_point, ball, zuma, len, 1);
        }
    }
}

以上为初版,仅供参考……

zuma_list(使用string类 实现)

标签:++   size   span   使用   using   namespace   operation   ret   index   

原文地址:https://www.cnblogs.com/19991201xiao/p/10529514.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!