#include <iostream>
#include <fstream>
#include <string>
using namespace std;
long dfs(string s1, string s2, int n);
long comb(int n, int m);
int main()
{
int n;
string s1, s2;
#ifndef ONLINE_JUDGE
ifstream cin("d:\\OJ\\uva_in.txt");
#endif
while (cin >> n >> s1 >> s2) {
cout << dfs(s1, s2, n) << endl;
}
return 0;
}
long dfs(string s1, string s2, int n)
{
if (s1.length() == 1) return 1;
else {
long sum = 1;
int count = 0;
string tmp1, tmp2;
s1 = s1.substr(1);
s2 = s2.substr(0, s2.length() - 1);
while (s1.length() != 0) {
string::size_type p = s2.find(s1[0]);
tmp1 = s1.substr(0, p + 1);
tmp2 = s2.substr(0, p + 1);
s1 = s1.substr(p + 1);
s2 = s2.substr(p + 1);
count++;
sum *= dfs(tmp1, tmp2, n);
}
return comb(n, count) * sum;
}
}
long comb(int n, int m)
{
long a = 1, b = 1;
for (int i = 0; i < m; i++) {
a *= (n - i);
b *= (m - i);
}
return a / b;
}
题目1044:Pre-Post
最新推荐文章于 2022-07-29 01:10:46 发布