We are given a pattern word w and a finite sequence of nonempty words C=(w1,…,wk). We want to select some words of C and concatenate them in the same order in which they appear in C (that is, choosing a strictly increasing sequence of indices) so that the concatenation equals the pattern w. Each word may be used at most once, and the chosen indices must be strictly increasing.
The pattern w and every word of C consist only of lowercase English letters ('a' to 'z'), carry no diacritics, and have length at most 150 each. The number of words satisfies 1≤k≤200.
For example, the pattern rytter can be formed from C=(ry,r,yt,y,tt,t,e,te,r,er) by choosing the words at indices (2, 4, 5, 7, 9), giving r + y + tt + e + r. Choosing indices (1, 5, 10), giving ry + tt + er, is another way to obtain the same pattern.
We want two things: how many such selections exist, and, among all of them, the lexicographically smallest one.
Write a program that:
NIE if there is no way to form the pattern w by concatenating a strictly increasing selection of words of C;The lexicographic order on index sequences compares them element by element: at the first position where two sequences differ, the one with the smaller index is smaller, and if one sequence is a proper prefix of the other, the shorter one is smaller.
Print:
NIE when it is impossible to form the pattern from the words of C under the rules above; or