字符串题,基于cpp模板的声明,先声明了n个函数原型,其中T代替任何数据类型,然后给了m种变量的声明,又给了k 个函数的声明,用的是之前那m个变量,问每个函数分别能匹配多少个函数原型
先是字符串处理,然后暴力循环,用变量类型替换变量名,直接判断是否匹配然后统计答案即可
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <map>
#include <string>
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define maxn 1000009
#define ll long long
using namespace std;
int n, m, k;
map <string, string> mp;
struct cadongllas
{
int num;
string var[7];
string name;
}ask[1009], funk[1009];
bool equal (cadongllas x, cadongllas y)
{
if (x.num != y.num)
return 0;
if (x.name != y.name)
return 0;
rep (i, 1, x.num)
if (x.var[i] != y.var[i] && x.var[i] != "T")
return 0;
return 1;
}
int main ()
{
cin >> n;
rep (i, 1, n)
{
char blank[1009];
scanf ("%s", blank);
//if (blank[strlen (blank) - 1] == '(') blank[ strlen (blank) - 1] = 0; ask[i].name = string (blank); cout << ask[i].name << endl;
while (1)
{
char ch = getchar ();
if (ch == '(')
break;
if (ch != ' ')
ask[i].name += ch;
}
//cout << ask[i].name << endl;
string s;
getline (cin, s);
int len = s.length ();
int now = 0;
while (1)
{
while ((s[now] == ')' || s[now] == ',' || s[now] == ' ' ) && now < len)
now++;
if (now >= len)
break;
string nex;
while (1)
{
nex += s[now++];
if (s[now] == ')' || s[now] == ',' || s[now] == ' ' || now >= len)
break;
}
ask[i].var[ ++ ask[i].num] = nex;
//cout << "----" << nex << endl;
if (s[now] == ')')
break;
}
}
//rep (i, 1, n) { printf ("%d %d\n", i, ask[i].num); rep (j, 1, ask[i].num) cout << ask[i].var[j] << endl; }
cin >> m;
rep (i, 1, m)
{
string sa, sb;
cin >> sa >> sb;
mp[sb] = sa;
}
cin >> k;
getchar ();
rep (i, 1, k)
{
while (1)
{
char ch = getchar ();
if (ch == '(')
break;
if (ch != ' ')
funk[i].name += ch;
}
//cout << "name" << funk[i].name << endl;
string s;
getline (cin, s);
int len = s.length (), now = 0;
while (1)
{
while ((s[now] == ')' || s[now] == ',' || s[now] == ' ' ) && now < len)
now++;
if (now >= len)
break;
string nex;
while (1)
{
nex += s[now++];
if (s[now] == ')' || s[now] == ',' || s[now] == ' ' || now >= len)
break;
}
funk[i].var[ ++ funk[i].num] = mp[nex];
//cout << "----" << nex << endl;
if (s[now] == ')')
break;
}
//rep (p, 1, funk[i].num) cout << "===" << funk[i].var[p] << "===" << endl;
int ans = 0;
rep (j, 1, n)
if (equal (ask[j], funk[i]))
ans++;
cout << ans << endl;
}
return 0;
}