Uva 11988 Broken Keyboard STL+链表

两种方法,直接上代码

STL标准模板库

#include <iostream>
#include <list>
#include <algorithm>
#include <cstdio>
using namespace std;
const maxn=100000+5;
char str[maxn];
typedef list<char> L;
int main(){
while(gets(str)){
L l;
L::iterator p;
int len=strlen(str);
for(int i=0;i<len;i++){
if (str[i]=='[') p=l.begin();
else if(str[i]==']') p=l.end();
else l.insert(p,str[i]);
}
for(p=l.begin();p!=l.end();p++)
printf("%c",*p);
printf("\n");
}
return 0;
}

数组模拟链表

#include <cstdio>
#include <cstring>
const int maxn=100000+5;
int last,cur,next[maxn];//光标位于cur字号的后面
char s[maxn];
int main(){
while(scanf("%s",s+1)==1){
int n=strlen(s+1);//输入保存在s[1],s[2],s[3],后面
cur=last=0;
next[0]=0;//头节点指向空
/*
从1到n逐个遍历
*/
for (int i=1;i<=n;i++){
char ch=s[i];
if (ch=='[') cur=0;
else if (ch==']') cur=last;
//交换性伴侣
else {
next[i]=next[cur];//新来的结点插向gay2p的后者
next[cur]=i;//gay2p的前者插向新来的结点
if (cur==last) last=i;//更新"最后一个字符"的编号
cur=i;//改变搜索2p对
}
}
for(int i=next[0];i!=0;i=next[i])
printf("%c",s[i]);
printf("\n");
}
return 0;
}

  

上一篇:我的第一个python web开发框架(29)——定制ORM(五)


下一篇:Linux软件包安装