'''Author : seraph14'''
from cmath import isclose
from collections import defaultdict
import math
import sys, io, os
import heapq
if 'PyPy' in sys.version:
from _continuation import continulet
else:
import threading
input = sys.stdin.readline
hqp = heapq.heappop
hqs = heapq.heappush
def _print(*argv, sep=" "):
sys.stdout.write(sep.join(map(str, argv)) + "\n")
def inp():
return (int(input()))
def inlt():
return (list(map(int, input().split())))
def insr():
if input == sys.stdin.readline:
s = input()
return list(s.strip())
else:
s = input().decode()
return (list(s[:len(s)-2]))
def invr():
return (map(int, input().split()))
def get_bool(right):
return "YES" if right else "NO"
def solve():
pass
def main():
n, m, x = invr()
keyboard = []
for _ in range(n):
keyboard.append(insr())
q = inp()
T = insr()
key_to_coord = defaultdict(list)
key_to_dist = defaultdict(float)
for i in range(n):
for j in range(m):
key_to_coord[keyboard[i][j]].append((i, j))
for t in key_to_coord:
if t == "S":
continue
min_cost = float("inf")
for x1, y1 in key_to_coord[t]:
if "S" not in key_to_coord:
break
for x2, y2 in key_to_coord["S"]:
min_cost = min(min_cost, (x1-x2)**2 + (y1-y2)**2)
key_to_dist[t] = math.sqrt(min_cost) if min_cost != float("inf") else float("inf")
cost = 0
for t in T:
if (t.islower() and t not in key_to_coord) or (t.isupper() and t.lower() not in key_to_coord):
_print(-1)
return
if t.islower():
continue
if "S" not in key_to_coord:
_print(-1)
return
val = key_to_dist[t.lower()]
cost += 0 if math.isclose(val, x) or val < x else 1
_print(cost)
if __name__ == '__main__':
if 'PyPy' in sys.version:
def bootstrap(cont):
call, arg = cont.switch()
while True:
call, arg = cont.switch(
to=continulet(lambda _, f, args: f(*args), call, arg))
cont = continulet(bootstrap)
cont.switch()
main()
else:
sys.setrecursionlimit(1 << 30)
threading.stack_size(1 << 27)
main_thread = threading.Thread(target=main)
main_thread.start()
main_thread.join()
#include "bits/stdc++.h"
using namespace std;
int main(){
int n,m,x;
cin>>n>>m>>x;
char a[n][m];
bool flag=true;
vector<pair<int,int>> shift,poskey[26];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
cin>>a[i][j];
if(a[i][j]=='S'){
flag=false;
shift.emplace_back(i,j);
}
else{
poskey[a[i][j]-'a'].push_back(make_pair(i,j));
}
}
int q;
cin>>q;
string b;
cin>>b;
bool can[26];
memset(can,0,sizeof(can));
int ans=0;
for(int i=0;i<26;i++){
for(int j=0;j<poskey[i].size();j++){
for(auto [t,y]:shift){
if(sqrt(pow(t-poskey[i][j].first,2)+pow(y-poskey[i][j].second,2))<=x){can[i]=1; break;}
}
if(can[i]==1) break;
}
}
for(int i=0;i<q;i++){
if(poskey[tolower(b[i])-'a'].size()==0){cout<<"-1"; return 0;}
if(b[i]<='Z'){
if(flag){cout<<"-1"; return 0;}
if(can[b[i]-'A']==0) ans++;
}
}
cout<<ans;
return 0;
}
1143B - Nirvana | 1285A - Mezo Playing Zoma |
919B - Perfect Number | 894A - QAQ |
1551A - Polycarp and Coins | 313A - Ilya and Bank Account |
1469A - Regular Bracket Sequence | 919C - Seat Arrangements |
1634A - Reverse and Concatenate | 1619C - Wrong Addition |
1437A - Marketing Scheme | 1473B - String LCM |
1374A - Required Remainder | 1265E - Beautiful Mirrors |
1296A - Array with Odd Sum | 1385A - Three Pairwise Maximums |
911A - Nearest Minimums | 102B - Sum of Digits |
707A - Brain's Photos | 1331B - Limericks |
305B - Continued Fractions | 1165B - Polycarp Training |
1646C - Factorials and Powers of Two | 596A - Wilbur and Swimming Pool |
1462B - Last Year's Substring | 1608B - Build the Permutation |
1505A - Is it rated - 2 | 169A - Chores |
765A - Neverending competitions | 1303A - Erasing Zeroes |