#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define TASKNAME "abc"

enum {OK, WA, PE, JE, NUM};
const int MaxN = 26;
const char err [NUM] [MaxN] = {"OK", "WA", "PE", "JE"};

char s [MaxN + 1];
bool b [MaxN];
int a [MaxN];

FILE * fin, * fout, * fans;

void chkexit (char * msg, int code)
{
 fprintf (stderr, "%s: %s\n", err[code], msg);
 exit (code);
}

bool answer (FILE * f, int mask)
{
 char ans [MaxN];
 char ch;
 if (fscanf (f, " %s", ans) != 1)
  chkexit ("No answer given!", PE | mask);
 if (!strcmp (ans, "NO"))
 {
  if (fscanf (f, " %c", &ch) != EOF)
   chkexit ("Extra info present!", PE | mask);
  return false;
 }
 if (!strcmp (ans, "YES"))
 {
  int i, k;
  memset (b, 0, sizeof (b));
  for (i = 0; i < MaxN; i++)
  {
   if (fscanf (f, " %d", &k) != 1)
    chkexit ("Not enough numbers in permutation!", PE | mask);
   if (k < 1 || k > 26) chkexit ("Number not in range!", PE | mask);
   k--;
   if (b[k]) chkexit ("Duplicate number in permutation!", WA | mask);
   if (a[k] < i)
    chkexit ("Student doesn't know that much letters!", WA | mask);
   b[k] = true;
  }
  if (fscanf (f, " %c", &ch) != EOF)
   chkexit ("Extra info present!", PE | mask);
  return true;
 } 
 chkexit ("Answer should be \"YES\" or \"NO\"!", PE | mask);
}

int main (int argc, char * argv [])
{
 int i;
 bool res1, res2;
 assert (argc >= 4);
 if ((fin = fopen (argv[1], "r")) == NULL) chkexit ("No input file!", JE);
 if ((fout = fopen (argv[2], "r")) == NULL) chkexit ("No output file!", PE);
 if ((fans = fopen (argv[3], "r")) == NULL) chkexit ("No answer file!", JE);
 fscanf (fin, " %s", s);
 for (i = 0; i < MaxN; i++)
  a[i] = s[i] - 'A';
 res1 = answer (fout, OK);
 res2 = answer (fans, JE);
 if ( res1 && !res2)
  chkexit ("Contestant can order students but jury can't!", JE);
 if (!res1 &&  res2)
  chkexit ("Jury can order students but contestant can't!", WA);
 if ( res1 &&  res2)
  chkexit ("Contestant and jury both can order students!", OK);
 if (!res1 && !res2)
  chkexit ("Contestant and jury both can't order students!", OK);
 assert (false);
 return 0;
}
