#include #include #define MAX_LEN (40) #define MAX_N (20) #define MAX_TOTAL_LEN (MAX_N * (MAX_LEN + 1)) typedef enum {False, True} Boolean; typedef struct { int next[2]; Boolean final; } Aut_Node; const Aut_Node posso = {{0, 0}, False}; Aut_Node aut[2][MAX_TOTAL_LEN + 1]; int n_states[2], n_words[2]; void read_aut(int i) { int j, k, c, s; char w[MAX_LEN + 1]; aut[i][0] = aut[i][1] = posso; n_states[i] = 2; for (j = 0; j < n_words[i]; ++j) { scanf("%s", w); s = 1; for (k = 0; k < strlen(w); ++k) { Aut_Node *state = &aut[i][s]; c = w[k]-'0'; if (state->next[c] == 0) { aut[i][n_states[i]] = posso; state->next[c] = n_states[i]++; } s = state->next[c]; } aut[i][s].final = True; } } int pairs[MAX_TOTAL_LEN * MAX_TOTAL_LEN+1][2]; int first_pair, n_pairs; void add_pair(int s0, int s1) { int i; if ((s0 == 0) || (s1 == 0)) return; for (i = 0; i < n_pairs; ++i) { if ((s0 == pairs[i][0]) && (s1 == pairs[i][1])) return; } pairs[n_pairs][0] = s0; pairs[n_pairs][1] = s1; ++n_pairs; } Boolean inter() { Aut_Node *s[2]; int i, c; while (first_pair < n_pairs) { for (i = 0; i < 2; ++i) { s[i] = & (aut[i][pairs[first_pair][i]]); } if (s[0]->final && s[1]->final) return True; for (c = 0; c < 2; ++c) { add_pair(s[0]->next[c], s[1]->next[c]); } if (s[0]->final) { for(c = 0; c < 2; ++c) { add_pair(1, pairs[first_pair][1]); } } if (s[1]->final) { for(c = 0; c < 2; ++c) { add_pair(pairs[first_pair][0], 1); } } ++first_pair; } return False; } int main(int argc, char *noargs[]) { int i; char res; for (;;) { if (scanf("%d %d", &n_words[0], &n_words[1]) == EOF) break; for (i = 0; i < 2; ++i) { read_aut(i); } pairs[0][0] = pairs[0][1] = 1; n_pairs = 1; first_pair = 0; res = inter() ? 'S' : 'N'; printf ("%c\n", res); } return 0; }