zuma_list(使用string类 实现)

#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);
        }
    }
}

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

上一篇:HDU 1556 Color the ball


下一篇:Ball CodeForces - 12D (线段树)