zoukankan      html  css  js  c++  java
  • [模拟]ZOJ3480 Duck Typing

    题意:给了一坨...按题目意思输出就好了...

    给一组案例

    100
    begin
    class d
    class c:d
    class b:c
    class a:b
    def d.m
    def d.n
    call a.m
    end

    答案应该是

    class dclass c:dclass b:cclass a:b
    def d.m
    def d.n
    invoke d.m

    建立的类应该是这样的

    a:b:c:d

    d有method m和n

    那么call a、b、c的n、m时都应invoke d的

      1 map<string, bool> mp_class, mp_method;
      2 map<string, string> mp_super[100005];
      3 map<string, int> s_num;
      4 bool dfs(string name, string method)
      5 {
      6     if(mp_method[name+"."+method])
      7     {
      8         printf("invoke ");
      9         cout<<name<<"."<<method<<endl;
     10         return true;
     11     }
     12     for(int i=0;i<s_num[name];i++)
     13         if(dfs(mp_super[i][name], method))
     14             return true;
     15     return false;
     16 }
     17 int main()
     18 {
     19     int t;
     20     scanf("%d", &t);
     21     while(t--)
     22     {
     23         getchar();
     24         char s[10005];
     25         mp_class.clear();
     26         mp_method.clear();
     27         s_num.clear();
     28         while(gets(s))
     29         {
     30             if(s[0]=='b')
     31                 continue;
     32             if(s[0]=='e')
     33                 break;
     34             int n=strlen(s);
     35             if(s[0]=='c' && s[1]=='l')   // class
     36             {
     37                 int i;
     38                 for(i=6;i<n;i++)
     39                     if(s[i]==':')
     40                         break;
     41                 if(i==n)           // ding yi
     42                 {
     43                     string name="";
     44                     for(int j=6;j<n;j++)
     45                         name+=s[j];
     46                     if(mp_class[name]==0)
     47                     {
     48                         mp_class[name]=1;
     49                         puts(s);
     50                     }
     51                     else
     52                         puts("oops!");
     53                 }
     54                 else
     55                 {
     56                     string name="", super="";
     57                     for(int j=6;j<i;j++)
     58                         name+=s[j];
     59                     for(int j=i+1;j<n;j++)
     60                         super+=s[j];
     61                     if(mp_class[name] || !mp_class[super])
     62                         puts("oops!");
     63                     else
     64                     {
     65                         mp_class[name]=1;
     66                         mp_super[s_num[name]++][name]=super;
     67                         puts(s);
     68                     }
     69                 }
     70             }
     71             else if(s[0]=='d')  // def
     72             {
     73                 int i;
     74                 for(i=0;i<n;i++)
     75                     if(s[i]=='.')
     76                         break;
     77                 string name="";
     78                 for(int j=4;j<i;j++)
     79                     name+=s[j];
     80                 string method=name+".";
     81                 for(int j=i+1;j<n;j++)
     82                     method+=s[j];
     83                 if(!mp_class[name])
     84                     puts("oops!");
     85                 else if(mp_method[method])
     86                 {
     87                     printf("redef ");
     88                     cout<<method<<endl;
     89                 }
     90                 else
     91                 {
     92                     mp_method[method]=1;
     93                     printf("def ");
     94                     cout<<method<<endl;
     95                 }
     96             }
     97             else if(s[0]=='u')  // undef
     98             {
     99                 string name="";
    100                 for(int i=6;i<n;i++)
    101                     name+=s[i];
    102                 if(!mp_method[name])
    103                     puts("oops!");
    104                 else
    105                 {
    106                     printf("undef ");
    107                     cout<<name<<endl;
    108                     mp_method[name]=0;
    109                 }
    110             }
    111             else                // call
    112             {
    113                 int i;
    114                 for(i=5;i<n;i++)
    115                     if(s[i]=='.')
    116                         break;
    117                 string na="";
    118                 for(int j=5;j<i;j++)   // class
    119                     na+=s[j];
    120                 string name="";
    121                 for(int j=i+1;j<n;j++)  // method
    122                     name+=s[j];
    123                 if(mp_method[na+"."+name])
    124                 {
    125                     printf("invoke ");
    126                     cout<<na<<"."<<name<<endl;
    127                     continue;
    128                 }
    129                 if(!dfs(na, name))
    130                     puts("oops!");
    131 //                bool flag=0;
    132 //                string cur=na;
    133 //                while(s_num[na])
    134 //                {
    135 //                    for(int k=0;k<s_num[na];k++)
    136 //                        if(mp_method[mp_super[k][na]+"."+name])
    137 //                        {
    138 //                            printf("invoke ");
    139 //                            cout<<mp_super[k][na]<<"."<<name<<endl;
    140 //                            flag=1;
    141 //                            break;
    142 //                        }
    143 //                }
    144 //                if(!flag)
    145 //                    puts("oops!");
    146             }
    147         }
    148         puts("");
    149     }
    150     return 0;
    151 }
    ZOJ 3480
  • 相关阅读:
    软工小白菜的团队介绍和采访
    团队作业第二次——团队Github实战训练
    团队作业第一次—团队展示和项目展示
    week5:Technology: Internets and Packets
    week3:History: The Web Makes it Easy to Use
    week2:History: The First Internet
    week4:History: Commercialization and Growth
    week1:History: Dawn of Electronic Computing
    第二日会议博客
    第一日会议博客
  • 原文地址:https://www.cnblogs.com/Empress/p/4391884.html
Copyright © 2011-2022 走看看