uva 11988 - Broken Keyboard (a.k.a. Beiju Text)
链表的应用;
le[i]表示第i个元素左边的那个元素的标号;
ri[i]表示第i个元素右边的那个元素的标号;
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstring> 3 #define maxn 100009 4 using namespace std; 5 6 char s[maxn]; 7 int le[maxn],ri[maxn]; 8 9 int main() 10 { 11 while(gets(s+1)!=NULL) 12 { 13 le[0]=ri[0]=0; 14 for(int i=1; s[i]; i++) 15 { 16 if(s[i]=='[')le[i]=0,ri[i]=ri[0]; 17 else if(s[i]==']')le[i]=le[0],ri[i]=0; 18 else le[i]=i-1,ri[i]=ri[i-1]; 19 le[ri[i]]=i,ri[le[i]]=i; 20 } 21 for(int i=ri[0]; i!=0; i=ri[i]) 22 if(s[i]!='['&&s[i]!=']') 23 putchar(s[i]); 24 puts(""); 25 } 26 return 0; 27 }