题目
假如有3位选手参赛,则我们分别用大写字母A、B、C表示三人,而这也是三位选手的答题顺序。他们的答题过程我们用一个字符串表示,字符串中有0~9十个数字及#号。0~9十个数字分别代表选手针对圆周率相应数字所答出诗句。如果数字相同,则代表该选手答对了,继续在场上等待作答;如果数字不同,则代表该选手答错了,判输并退出作答。如果出现#号则代表相应选手未能在限定时间内作答,同样判输并退出作答。如果有选手未能“飞”出针对某数字的诗词,则后续的下一位选手继续就该数字“飞花”。当场上只留下一位选手时,他她就是最后的胜利者。现在请你输出谁是最后的胜利者。
输入
输入数据有三行,第一行为选手个数n(1<n<6);第二行为圆周率,小数点后的数字不超过200位,长度足够判断出胜利者;第3行为n位选手的作答过程。
输出
输出代表胜利者的大写字母。
样例输入
3
3.141592653589
314145926#
样例输出
A
先将双向循环链表把选手连成一个圈,然后遍历string每个字符模拟答题
过程
#include<iostream>
#include<string>
using namespace std;
struct node
{
char ch;
struct node *pre,*next;
};
int main(){
int n;
cin>>n;
string a,b;
cin>>a>>b;
struct node* p=(struct node*)malloc(sizeof(struct node));
p->ch='A';
p->next=NULL;
p->pre=NULL;
struct node* head=p;
for(int i=1;i<n;i++) //将几个选手用双向循环链表连起来
{
struct node* tmp=(struct node*)malloc(sizeof(struct node));
tmp->ch='A'+i;
tmp->next=NULL;
tmp->pre=p;
p->next=tmp;
p=tmp;
}
head->pre=p;
p->next=head;
p=head;
int i=0,j=0,cnt=n;
while (cnt>1)
{
if(a[i]<'0'||a[i]>'9')
{
i++;
continue;
}
if(a[i]==b[j])
{
i++;
j++;
p=p->next;
}
else
{
j++;
p->next->pre=p->pre;
p->pre->next=p->next;
p=p->next;
cnt--;
}
}
cout<<p->ch;
return 0;
}