progress

I'd rather be anything but ordinary

0%

PA_Zuma

今天更新道作业题,题目是TsingHua oj的Programming Assignment#1

原题链接点此

本题题意很简单就是让相邻超过3的小球消失。由于清华oj禁止STL的使用,自然不能用string,想想也对,练习数据结构用成熟的库就没意思了。此题由于插入、删除操作较多,适合使用链表,不过我是用向量实现的,等以后将链表实现补上。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char a[10000 + 10];
int len;
void insert(int r,int e){
for (int i = len; i > r;i--)
a[i] = a[i - 1];
a[r] = e;
a[len++] = '\0';
}
void remove(int lo,int hi){
if(lo==hi) return;
while(hi<len)
a[lo++] = a[hi++];
a[lo] = '\0';
len -= (hi - lo);
}
int find (int &r){
int lo = r, hi = r;
char elem = a[r];
while(a[lo]== elem && lo) lo--;
if(lo || a[lo]!= elem) lo++;
while(a[hi]==elem && hi<len) hi++;
if(hi-lo>2){
remove(lo, hi);
r = lo;
return 1;
}
return 0;
}
int main(){
//freopen("input.txt", "r", stdin);
scanf("%s", &a);
int n; len = strlen(a);
scanf("%d", &n);
while (n--)
{
int idx;
char c;
scanf("%d %c", &idx, &c);
insert(idx, c);
while(find(idx)&&len>2) ;
if(len==0)
printf("%c\n", '-');
else
printf("%s\n", a);
}
}