88B.md (2753B)
1 Source: https://codeforces.com/problemset/problem/88/B 2 3 B. Keyboard 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 stdin 16 17 output 18 19 stdout 20 21 Vasya learns to type. He has an unusual keyboard at his disposal: it is rectangular and it has *n* rows of keys containing *m* keys in each row. Besides, the keys are of two types. Some of the keys have lowercase Latin letters on them and some of the keys work like the "Shift" key on standard keyboards, that is, they make lowercase letters uppercase. 22 23 Vasya can press one or two keys with one hand. However, he can only press two keys if the Euclidean distance between the centers of the keys does not exceed *x*. The keys are considered as squares with a side equal to 1. There are no empty spaces between neighbouring keys. 24 25 Vasya is a very lazy boy, that's why he tries to type with one hand as he eats chips with his other one. However, it is possible that some symbol can't be typed with one hand only, because the distance between it and the closest "Shift" key is strictly larger than *x*. In this case he will have to use his other hand. Having typed the symbol, Vasya returns other hand back to the chips. 26 27 You are given Vasya's keyboard and the text. Count the minimum number of times Vasya will have to use the other hand. 28 29 Input 30 31 The first line contains three integers *n*, *m*, *x* (1 ≤ *n*, *m* ≤ 30, 1 ≤ *x* ≤ 50). 32 33 Next *n* lines contain descriptions of all the keyboard keys. Each line contains the descriptions of exactly *m* keys, without spaces. The letter keys are marked with the corresponding lowercase letters. The "Shift" keys are marked with the "S" symbol. 34 35 Then follow the length of the text *q* (1 ≤ *q* ≤ 5·105). The last line contains the text *T*, which consists of *q* symbols, which are uppercase and lowercase Latin letters. 36 37 Output 38 39 If Vasya can type the text, then print the minimum number of times he will have to use his other hand. Otherwise, print "-1" (without the quotes). 40 41 Examples 42 43 Input 44 45 ``` 46 2 2 1 47 ab 48 cd 49 1 50 A 51 ``` 52 53 Output 54 55 ``` 56 -1 57 ``` 58 59 Input 60 61 ``` 62 2 2 1 63 ab 64 cd 65 1 66 e 67 ``` 68 69 Output 70 71 ``` 72 -1 73 ``` 74 75 Input 76 77 ``` 78 2 2 1 79 ab 80 cS 81 5 82 abcBA 83 ``` 84 85 Output 86 87 ``` 88 1 89 ``` 90 91 Input 92 93 ``` 94 3 9 4 95 qwertyuio 96 asdfghjkl 97 SzxcvbnmS 98 35 99 TheQuIcKbRoWnFOXjummsovertHeLazYDOG 100 ``` 101 102 Output 103 104 ``` 105 2 106 ``` 107 108 Note 109 110 In the first sample the symbol "A" is impossible to print as there's no "Shift" key on the keyboard. 111 112 In the second sample the symbol "e" is impossible to print as there's no such key on the keyboard. 113 114 In the fourth sample the symbols "T", "G" are impossible to print with one hand. The other letters that are on the keyboard can be printed. Those symbols come up in the text twice, thus, the answer is 2.