这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。
题目地址: http://coder.buct.edu.cn/oj/Problem.aspx?pid=1104
对C++中stack容器熟悉。
#include
#include
#include
#include
#include
#include
using namespace std;
stack _stack;
char str[1010];
int main()
{
//freopen("input.txt", "r", stdin);
while(scanf("%s", str) != EOF)
{
while(!_stack.empty())
_stack.pop();
int cnt=1;
int flag=0;
//printf("strlen=%d\n", strlen(str));
for(int i=0; i<strlen(str); ++i)
{
if(str[i]=='P')
_stack.push(cnt++);
else if(str[i]=='Q')
{
if(flag)
printf(" ");
flag=1;
if(_stack.empty())
{
printf("error");
break;
}
else
{
printf("%d", _stack.top());
_stack.pop();
}
}
}
printf("\n");
}
}