链接:https://www.nowcoder.com/acm/contest/74/A
解法:
#include<stdio.h> #include<string.h> char s[200],a[200]; int len,h; void cl(char ch) { if(h==0) { h++; a[h]=ch; } else { if(ch=='o')//'oO' { if(a[h]=='O') { h++; a[h]='o'; } else if(a[h]=='o') { a[h]='z'; h--; cl('O'); } } if(ch=='O') { if(a[h]=='O') { a[h]='z'; h--; } else if(a[h]=='o') { h++; a[h]='O'; } } } return; } int main() { while(~scanf("%s",s)) { h=0; len=strlen(s); for(int i=0;i<len;i++) { cl(s[i]); } for(int i=0;i<=len;i++) { if(a[i]=='O'||a[i]=='o') printf("%c",a[i]); } printf("\n"); memset(a,'0',sizeof(a)); } return 0; }
关键在于运用栈的思想