题目链接:https://www.luogu.com.cn/problem/P7369
思路:
每次输入巫师决斗时,先判断当前老魔杖所效忠的巫师有无参与决斗
若参与决斗,那么老魔杖换人效忠;若无,不换人
同时,注意之前效忠过的巫师可能会再次效忠。所以用一个数组进行记录,新效忠的巫师之前是否效忠过
代码如下:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main ( )
{
char x, y, z, ans[ 91 ];
int n, sum = 1;
cin >> x >> n;
memset ( ans, 0, sizeof ( ans ) );
ans[ x ] = 1;
while ( n -- )
{
cin >> y >> z;
if ( z == x )
{
x = y;
if ( !ans[ y ] )
{
ans[ y ] = 1;
++ sum;
}
}
}
cout << x << endl;
cout << sum << endl;
return 0;
}