CodeForces - 200DProgramming Language純類比

來源:互聯網
上載者:User

標籤:

CodeForces - 200DProgramming Language
Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u

Submit Status

Description

Recently, Valery have come across an entirely new programming language. Most of all the language attracted him with template functions and procedures. Let us remind you that templates are tools of a language, designed to encode generic algorithms, without reference to some parameters (e.g., data types, buffer sizes, default values).

Valery decided to examine template procedures in this language in more detail. The description of a template procedure consists of the procedure name and the list of its parameter types. The generic type T parameters can be used as parameters of template procedures.

A procedure call consists of a procedure name and a list of variable parameters. Let‘s call a procedure suitable for this call if the following conditions are fulfilled:

  • its name equals to the name of the called procedure;
  • the number of its parameters equals to the number of parameters of the procedure call;
  • the types of variables in the procedure call match the corresponding types of its parameters. The variable type matches the type of a parameter if the parameter has a generic type T or the type of the variable and the parameter are the same.

You are given a description of some set of template procedures. You are also given a list of variables used in the program, as well as direct procedure calls that use the described variables. For each call you need to count the number of procedures that are suitable for this call.

Input

The first line contains a single integer n (1?≤?n?≤?1000) — the number of template procedures. The next n lines contain the description of the procedures specified in the following format:

"void procedureName (type_1, type_2, ..., type_t)" (1?≤?t?≤?5), where void is the keyword, procedureName is the procedure name, type_i is the type of the next parameter. Types of language parameters can be "int", "string", "double", and the keyword "T", which denotes the generic type.

The next line contains a single integer m (1?≤?m?≤?1000) — the number of used variables. Next m lines specify the description of the variables in the following format:

"type variableName", where type is the type of variable that can take values "int", "string", "double", variableName — the name of the variable.

The next line contains a single integer k (1?≤?k?≤?1000) — the number of procedure calls. Next k lines specify the procedure calls in the following format:

"procedureName (var_1, var_2, ..., var_t)" (1?≤?t?≤?5), where procedureName is the name of the procedure, var_i is the name of a variable.

The lines describing the variables, template procedures and their calls may contain spaces at the beginning of the line and at the end of the line, before and after the brackets and commas. Spaces may be before and after keyword void. The length of each input line does not exceed100 characters. The names of variables and procedures are non-empty strings of lowercase English letters and numbers with lengths of not more than 10 characters. Note that this is the only condition at the names. Only the specified variables are used in procedure calls. The names of the variables are distinct. No two procedures are the same. Two procedures are the same, if they have identical names and identical ordered sets of types of their parameters.

Output

On each of k lines print a single number, where the i-th number stands for the number of suitable template procedures for the i-th call.

Sample Input

Input
4void f(int,T)void  f(T, T) void foo123   ( int,  double,  string,string  )  void  p(T,double)3int a string    sdouble x123 5f(a,  a) f(s,a   )foo   (a,s,s) f  (  s  ,x123)proc(a)
Output
21010
Input
6void f(string,double,int)void f(int) void f  ( T  )void procedure(int,double)void f  (T, double,int)   void f(string, T,T)4 int a int xstring  tdouble  val 5f(t, a, a)f(t,val,a)f(val,a, val) solve300(val, val)f  (x)
Output
13002

Source

Codeforces Round #126 (Div. 2)這道題目首先告訴大家,要有耐心現在說一下我解題思路,可能麻煩,如果有大神飄過,求指點先將void f(int , T)變為voidf(int,T)將所有的空格都去掉,然後將字串的前面四個去掉因為void不是函數名,在‘(‘左邊就是函數名。接著其他的處理也是類似的類比
/*Author: 2486Memory: 256 KBTime: 62 MSLanguage: GNU G++ 4.9.2Result: Accepted*/#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <map>#include <string>#include <iostream>using namespace std;const int maxn=1000+5;struct func {    char name[100+5];    vector<int>G;} funcs[maxn];int n,m;map<string,int>F;map<string,bool>KS;char str[maxn];void getgs(char *st,int l) {    int cnt=0;    for(int i=0; i<l; i++) {        if(st[i]!=' ') {            st[cnt++]=st[i];        }    }    st[cnt]='\0';}void getsg(char *st,int id) {    int l=strlen(st);    int cnt=0;    for(int i=4; i<l; i++) {        if(st[i]=='(')break;        funcs[id].name[cnt++]=st[i];    }    funcs[id].name[cnt]='\0';    for(int i=cnt+4; i<l; i++) {        if(str[i-1]=='n'&&st[i]=='t') {            funcs[id].G.push_back(1);        } else if(str[i-1]=='d'&&st[i]=='o') {            funcs[id].G.push_back(2);        } else if(str[i-1]=='s'&&st[i]=='t') {            funcs[id].G.push_back(3);        } else if(str[i]=='T') {            funcs[id].G.push_back(4);        }    }}int getsgs(char *st) {    func s;    int l=strlen(st);    int cnt=0;    for(int i=0; i<l; i++) {        if(st[i]=='(')break;        s.name[cnt++]=st[i];    }    s.name[cnt]='\0';    if(!KS[s.name])return 0;    char op[100+5];    int sf=0;    for(int i=cnt+1; i<l; i++) {        if(st[i]==','||st[i]==')') {            op[sf]='\0';            s.G.push_back(F[op]);            sf=0;            continue;        }        op[sf++]=st[i];    }    int res=0;    bool flag;    for(int i=0; i<n; i++) {        if(strcmp(funcs[i].name,s.name)==0) {            flag=true;            if(s.G.size()!=funcs[i].G.size())continue;            for(int j=0; j<s.G.size(); j++) {                if(funcs[i].G[j]==4)continue;                if(funcs[i].G[j]!=s.G[j]) {                    flag=false;                    break;                }            }            if(flag)res++;        }    }    return res;}int main() {    //freopen("D://imput.txt","r",stdin);    scanf("%d",&n);    for(int i=0; i<n; i++) {        gets(str);        int l=strlen(str);        if(l==0) {            i--;            continue;        }        getgs(str,l);        getsg(str,i);        KS[funcs[i].name]=1;    }    scanf("%d",&m);    char op[10],ops[100+5];    for(int i=0; i<m; i++) {        scanf("%s%s",op,ops);        if(op[0]=='d'&&op[1]=='o') {            F[ops]=2;        } else if(op[0]=='i'&&op[1]=='n'&&op[2]=='t') {            F[ops]=1;        } else if(op[0]=='s'&&op[1]=='t') {            F[ops]=3;        }    }    scanf("%d",&m);    for(int i=0; i<m; i++) {        gets(str);        int l=strlen(str);        if(l==0) {            i--;            continue;        }        getgs(str,l);        printf("%d\n",getsgs(str));    }    return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

CodeForces - 200DProgramming Language純類比

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.