File size: 1,195 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
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
/*
****************************************************************
****************************************************************
-> Coded by Stavros Chryselis				   
-> Visit my github for more solved problems over multiple sites 
-> https://github.com/StavrosChryselis			  
-> Feel free to email me at [email protected]	
****************************************************************
****************************************************************
*/
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <climits>
 
using namespace std;
 
string A, B;
int N, M;
 
inline void init()
{
	cin >> N >> M;
	cin >> A;
	cin >> B;
}
 
inline int solve()
{
	int memoA[26], memoB[26];
	int i, rval = INT_MAX;
 
	memset(memoA, 0, sizeof(memoA));
	memset(memoB, 0, sizeof(memoB));
 
 
	for (i = 0; i < N; i++)
		memoA[A[i] - 'a']++;
 
	for (i = 0; i < M; i++)
		memoB[B[i] - 'a']++;
 
	for (i = 0; i < 26; i++)
		if (memoB[i])
			rval = min(rval, memoA[i] / memoB[i]);
 
	return rval;
}
 
int main()
{
//	freopen("input.txt", "r", stdin);
	ios::sync_with_stdio(0);
 
	int T;
 
	cin >> T;
 
	while (T--)
	{
		init();
		cout << solve() << "\n";
	}
 
	return 0;
}