- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
#include <bits/stdc++.h>
using namespace std;
vector <pair<int,int> > vpp;
long long vres = 0;
int get_dist(pair<int,int> l, pair<int,int> r)
{
int ans1 = abs(l.first - r.first);
int ans2 = abs(l.second - r.second);
return ans1 + ans2;
}
pair<vector <pair<int,int> > , long long> rec(const string &s, int id,long long len,pair<int,int> pos,vector <vector <pair<int,int> > > &cl, vector <pair<int,int> > p)
{
if(id == (int)s.size())
return {p,len};
int st = s[id] - 'a';
pair<vector <pair<int,int> > , long long> ans,tmp;
for(int j = 0; j < (int)cl[st].size(); ++j)
{
p.push_back({cl[st][j]});
tmp = rec(s,id + 1, len + get_dist(pos, cl[st][j]), cl[st][j], cl,p);
if(tmp.second >= ans.second)
{
ans = tmp;
}
p.pop_back();
}
return ans;
}
#define mag pair< pair< vector< pair<int,int> > , long long> , pair< pair< int,int >,pair< int,int > > >
mag raz(int l, int r,const string &s,vector <vector <pair<int,int> > > &cl)
{
mag v1,v2;
vector <pair<int,int> > p;
if(r - l >= 1)
{
int tm = (l + r) >> 1;
v1 = raz(l,tm,s,cl);
bool f = 1;
if(tm + 1 > r)
{
v2 = v1;
f = 0;
}
else
v2 = raz(tm + 1,r,s,cl);
// int st1 = ar[v1.second.first.first][v1.second.first.second];
// int st2 = ar[v2.second.first.first][v2.second.first.second];
long long len = 0;
///merge
int n = (int)v1.first.first.size();
len += v1.first.second;
for(int i = 0; i < n; ++i)
{
p.push_back(v1.first.first[i]);
}
if(f)
{
len += v2.first.second;
int n = (int)v2.first.first.size();
for(int i = 0; i < n; ++i)
{
p.push_back(v2.first.first[i]);
}
}
len += get_dist(v1.second.second, v2.second.first);
return {{p,len}, {v1.second.second, v2.second.first}};
}
int st = s[l] - 'a';
int x1 = rand() % (int)cl[st].size();
p.push_back(cl[st][x1]);
// cout << cl[st].size() << " " << l << " " << r <<'\n';
return {{p,0},{cl[st][x1],cl[st][x1]}};
}
//pair<int,vector <pair<int,int> > > solve(ifstream &cin, ofstream &cout)
void solve(ifstream &cin, ofstream &cout)
{
vector <int> used(26);
vector <vector <pair<int,int> > > cl(26);
int n, m ,l;
cin >> n >> m >> l;
vector <vector <char > > ar(n,vector <char> (m));
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < m; ++j)
{
cin >> ar[i][j];
}
}
string s;
cin >> s;
for(int i = 0; i < n; ++i)
{