RT
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ite vector <card> :: iterator
struct card{
bool notused;
char card;
};
#define for_i_in_cards for(ite i = cards.begin();i != cards.end();i++)
struct Pig{
void init(char shenfen);
void start_huihe();
void mopai();
void chupai();
void usecard(ite i , char c);
bool finduse(char c);
int notplushealth();
int plushealth();
void jump();
void go_classpig();
void return_classpig();
void unequip();
void hurt(Pig &that);
bool will_die();
void killpig(Pig &that);
void mekill();
bool canuse(char c);
int try_kill();
void kill(Pig &that);
bool xiangying_kill();
int try_fight();
void fight(Pig &that);
void nanman();
bool xiangying_nanman();
void arrow();
bool xiangying_arrow();
void equip();
bool use_wuxie(Pig &that);
bool xiangying_wuxie();
int id;
vector <card> cards;
char shenfen;
int health;
bool is_equip , die , use_kill , jumped , isclass;
};
struct init{
void mian_input();
void cin_n_m();
void input_card();
void input_heap();
};
int n , m , main_pig , fan_num , top = 1;
bool over;
char win , Heap[100005];
Pig pig[105];
init in;
void maped();
void copy();
void initpig();
void print();
signed main(){
in.mian_input();
initpig();
maped();
copy();
pig[main_pig].jump();
if(fan_num == 0){
win = 'M';
print();
return 0;
}
int i = 0;
while(1){
i++;
if(i > n) i -= n;
if(pig[i].die) continue;
pig[i].start_huihe();
if(over){
print();
return 0;
}
}
}
void maped(){
for(int i = 1;i <= n;i++){
pig[i].id = i;
}
}
void copy(){
char op = Heap[m];
for(int i = m + 1;i <= 10000;i++){
Heap[i] = op;
}
}
void print(){
if(win == 'M') puts("MP");
else puts("FP");
for(int i = 1;i <= n;i++){
if(pig[i].die){
puts("DEAD");
continue;
}
Pig now = pig[i];
for(ite i = now.cards.begin();i != now.cards.end();i++){
if(i -> notused) cout << i -> card << ' ';
}
puts("");
}
}
void initpig(){
for(int i = 1;i <= n;i++){
pig[i].health = 4;
pig[i].is_equip = pig[i].isclass = pig[i].die = pig[i].use_kill = pig[i].jumped = 0;
}
}
void init::mian_input(){
cin_n_m();
input_card();
input_heap();
}
void init::cin_n_m(){
cin >> n >> m;
}
void init::input_card(){
string s;
char c;
for(int i = 1;i <= n;i++){
cin >> s;
if(s[0] == 'M'){
main_pig = i;
}
else if(s[0] == 'F'){
fan_num++;
}
pig[i].init(s[0]);
c = getchar();
while(c != '\n'){
if(c >= 'A' && c <= 'Z'){
pig[i].cards.push_back(card{1 , c});
}
c = getchar();
}
}
}
void init::input_heap(){
char c;
for(int i = 1;i <= m;i++){
c = getchar();
while(c < 'A' || c > 'Z'){
c = getchar();
}
Heap[i] = c;
}
}
void Pig::init(char id){
shenfen = id;
}
void Pig::start_huihe(){
mopai();
mopai();
chupai();
use_kill = 0;
}
void Pig::mopai(){
cards.push_back(card{1 , Heap[top++]});
}
void Pig::chupai(){
bool flag = 1;
while(flag){
flag = 0;
for(ite i = cards.begin();i != cards.end();i++){
if(i -> notused && canuse(i -> card)){
usecard(i , i -> card);
if(die || over){
i = cards.begin() - 1;
flag = 1;
}
}
}
}
}
bool Pig::finduse(char c){
for_i_in_cards{
if(i -> card == c && i -> notused){
i -> notused = 0;
return 1;
}
}
return 0;
}
void Pig::usecard(ite i , char c){
int tmp;
switch(c){
case 'K':
if(tmp = try_kill()){
i -> notused = 0;
kill(pig[tmp]);
}
break;
case 'P':
i -> notused = 0;
plushealth();
break;
case 'F':
if(tmp = try_fight()){
i -> notused = 0;
fight(pig[tmp]);
}
break;
case 'N':
i -> notused = 0;
nanman();
break;
case 'W':
i -> notused = 0;
arrow();
break;
case 'Z':
i -> notused = 0;
equip();
break;
}
}
inline int Pig::notplushealth(){
return --health;
}
inline int Pig::plushealth(){
return ++health;
}
inline void Pig::jump(){
jumped = 1;
}
inline void Pig::go_classpig(){
isclass = 1;
}
inline void Pig::return_classpig(){
isclass = 0;
}
inline void Pig::unequip(){
is_equip = 0;
}
inline void Pig::hurt(Pig &that){
that.notplushealth();
if(that.health == 0 && that.will_die()){
this -> killpig(that);//击杀处理
}
}
bool Pig::will_die(){
for(ite i = cards.begin();i < cards.end();i++){
if(i -> card == 'P' && i -> notused){
i -> notused = false;
plushealth();
return 0;
}
}
return 1;
}
void Pig::killpig(Pig &that){
that.mekill();
if(that.shenfen == 'M'){
win = 'F';
over = true;
}
else if(that.shenfen == 'F'){
fan_num--;
if(fan_num == 0){
win = 'M';
over = true;
return;
}
mopai();
mopai();
mopai();
}
else if(shenfen == 'M' && that.shenfen == 'Z'){
for(ite i = cards.begin();i != cards.end();i++){
i -> notused = false;
}
unequip();
}
}
inline void Pig::mekill(){
die = true;
}
bool Pig::canuse(char c){
if(c == 'P' && health < 4) return 1;
else if((c == 'K' && try_kill()) || (c == 'F' && try_fight()) || c == 'N' || c == 'W' || c == 'Z') return 1;
return 0;
}
int Pig::try_kill(){
if(use_kill && !is_equip) return 0;
if(shenfen == 'M'){
for(int i = id % n + 1;i != id;i = i % n + 1){
if(pig[i].die) continue;
if(pig[i].isclass || (pig[i].shenfen == 'F' && pig[i].jumped)){
return i;
}
else{
return 0;
}
}
}
else if(shenfen == 'Z'){
for(int i = id % n + 1;i != id;i = i % n + 1){
if(pig[i].die) continue;
if(pig[i].shenfen == 'F' && pig[i].jumped){
return i;
}
else{
return 0;
}
}
}
else{
int i;
for(i = id % n + 1;i != id;i = i % n + 1){
if(pig[i].die) continue;
if(pig[i].shenfen != 'F' && pig[i].jumped){
return i;
}
else{
return 0;
}
}
}
}
inline void Pig::kill(Pig &that){
jump();
return_classpig();
use_kill = 1;
if(!that.xiangying_kill()){
hurt(that);
}
}
inline bool Pig::xiangying_kill(){
return finduse('D');
}
int Pig::try_fight(){
if(shenfen == 'M'){
for(int i = id % n + 1;i != id;i = i % n + 1){
if(pig[i].die) continue;
if(pig[i].isclass || (pig[i].shenfen == 'F' && pig[i].jumped)){
return i;
}
}
}
else if(shenfen == 'Z'){
for(int i = id % n + 1;i != id;i = i % n + 1){
if(pig[i].die) continue;
if(pig[i].shenfen == 'F' && pig[i].jumped){
return i;
}
}
}
else{
return main_pig;
}
return 0;
}
void Pig::fight(Pig &that){
jump();
return_classpig();
if(use_wuxie(that)) return;
if(shenfen == 'M' && that.shenfen == 'Z'){
hurt(that);
return;
}
int hurted;
while(1){
if(!that.finduse('K')){
hurted = 2;
break;
}
if(!finduse('K')){
hurted = 1;
break;
}
}
if(hurted == 1){
that.hurt(*this);
}
else{
hurt(that);
}
}
void Pig::nanman(){
for(int i = id % n + 1;i != id;i = i % n + 1){
if(pig[i].die) continue;
if(use_wuxie(pig[i])) continue;
if(!pig[i].xiangying_nanman()){
hurt(pig[i]);
if(over) return;
if(pig[i].shenfen == 'M' && !jumped)//类反猪判定
{
go_classpig();
}
}
}
}
inline bool Pig::xiangying_nanman(){
return finduse('K');
}
void Pig::arrow(){
for(int i = id % n + 1;i != id;i = i % n + 1){
if(pig[i].die) continue;
if(use_wuxie(pig[i])) continue;
if(!pig[i].xiangying_arrow()){
hurt(pig[i]);
if(over) return;
if(pig[i].shenfen == 'M' && !jumped){
go_classpig();
}
}
}
}
inline bool Pig::xiangying_arrow(){
return finduse('D');
}
inline void Pig::equip(){
is_equip = true;
}
bool Pig::use_wuxie(Pig &that){
if(!that.jumped) return false;
bool flag = true;
for(int i = id;i != id || flag == 1;i = i % n + 1){
flag = false;
if(pig[i].die) continue;
if(that.shenfen == 'M' && pig[i].shenfen == 'F') continue;
if(that.shenfen == 'Z' && pig[i].shenfen == 'F') continue;
if(that.shenfen == 'F' && pig[i].shenfen == 'Z') continue;
if(that.shenfen == 'F' && pig[i].shenfen == 'M') continue;
if(pig[i].finduse('J')){
pig[i].jump();
return !pig[i].xiangying_wuxie();
}
}
return false;
}
bool Pig::xiangying_wuxie(){
for(int i = id % n + 1;i != id;i = i % n + 1){
if(pig[i].die) continue;
if(shenfen == 'M' && pig[i].shenfen == 'Z') continue;
if(shenfen == 'Z' && pig[i].shenfen == 'Z') continue;
if(shenfen == 'F' && pig[i].shenfen == 'F') continue;
if(shenfen == 'Z' && pig[i].shenfen == 'M') continue;
if(pig[i].finduse('J')){
pig[i].jump();
return !pig[i].xiangying_wuxie();
}
}
return false;
}