用递归来做,参考
#include <iostream>
#include <string>
using namespace std;
int n, m;
string s, str, q;
void pft(int k,long long &num)
{
char c;
string st;
if (k>=n)
{
if (q==str)
num++;
return;
}
if (s[k]=='-')
{
c = q.back();
q.pop_back();
pft(k+1,num);
q.push_back(c);
st = q.substr(0,1);
q.erase(0,1);
pft(k+1,num);
q.insert(0,st);
} else {
q.push_back(s[k]);
pft(k+1,num);
q.pop_back();
}
}
int main()
{
int t;
long long num;
cin >> t;
while (t--)
{
cin >> n >> m >> s >> str;
num = 0;
pft(0,num);
cout << num % (1000000007) << endl;
}
return 0;
}
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!