"ZOJ 3480" Duck Typing

Source: Internet
Author: User
Tags strcmp

Problem

Test instructions

1. There is a T group of data, the input of each set of data between the space between, the output also requires a space separated.

2. Each group is a line begin and the end of a line ends.

3.class Classname[:super] represents the declaration of a type, when a super value exists, indicating that it inherits the super type.

4.def Classname.method represents a method for declaring a ClassName type.

5.undef Classname.method means that the method is removed under that type.

6.call Classname.method means that the method is called, and if the type itself does not have the method, go to see if it has an ancestor.

Analysis

It can be handled with a string of strings, or it can be handled with char (the posture I write is not very good-looking, hand-covering).

Say a few easy wrong places: ... In fact, the position is different, the wrong place is different. The most afraid of is I wa in the "oops" spelled "Opps" Qaq. And, like, "." Don't write ":"!

Code char Processing
#include <cstdio> #include <cstring>struct cla{Char name[50]; int Mnum;//num of method char Md[50][50];//method int super;}    A[10050];void Read (char c,int &flag,char name[][50]) {int k=0;    int len=0;    int cnt=0;    flag=1; if (c== ' B ' | |        c== ' E ') flag=0;//begin/end while ((C=getchar ()) &&c!= ' \ n ') {cnt++;            if (cnt==1 && flag) {if (c== ' e ') flag=2;            else if (c== ' a ') flag=3;        else if (c== ' n ') flag=4; } if (flag==1 && cnt>5 | | Class ClassName flag==2 && cnt>3 | | def classname.method flag==3 && cnt>4 | | undef classname.method flag==4 && cnt>5 | |            Call Classname.method flag==5)//class sub:super {name[k][len]=c;                if (c== ': ') {name[k][len]= ');                Len=-1;                k=1;       flag=5;     } if (c== '. ')                {name[k][len]= ' + ';                Len=-1;            k=1;        } len++;    }}}int Check (int flag,int &mp,int &fp,char name[][50],int cnum) {int defined1=0,defined2=0;    int i,j;    for (I=1; i<=cnum; i++) if (strcmp (a[i].name,name[0]) ==0) defined1= i;        if (flag==1) {if (defined1) return-1;    return 0;        } if (flag==5) {if (defined1) return-1; else {for (i=1; i<=cnum; i++) if (strcmp (a[i].name,name[1]) ==0) Defi            Ned1= i;            if (defined1==0) return-1;        return defined1;    }} if (!defined1) return-1;    int f=defined1,ok=0;             while (F!=0&&!ok) {for (j=1; j<=a[f].mnum; J + +) {if (strcmp (a[f].md[j],name[1]) ==0)                {defined2=j;            ok=1;   }} Mp=defined2;     Fp=f;        if (flag==3) F=a[f].super;    else f=0;    } if (flag==2) {if (defined2) return-2;    } else if (!DEFINED2) return-1; return defined1;}        void work (int flag,int p,int mp,int fp,char name[][50],int &cnum) {if (flag==1) {cnum++;        strcpy (A[cnum].name,name[0]);        a[cnum].super=0;    printf ("Class%s\n", Name[0]);        } else if (flag==2) {if (p==-2) {printf ("Redef%s.%s\n", name[0],name[1]);            } else {strcpy (a[p].md[++a[p].mnum],name[1]);        printf ("Def%s.%s\n", A[p].name,name[1]);    }} else if (flag==3) {printf ("Invoke%s.%s\n", a[fp].name,name[1]);        } else if (flag==4) {memset (a[fp].md[mp],0,sizeof (A[FP].MD[MP]));    printf ("Undef%s.%s\n", name[0],name[1]);        } else if (flag==5) {cnum++;        strcpy (A[cnum].name,name[0]);        A[cnum].super=p;    printf ("Class%s:%s\n", Name[0],name[1]); }}int main () {int t;    scanf ("%d", &t);        while (t--) {int cnum=0;                int flag;        MP is the method in which the type of method is the first of several//fp is the type of the method that belongs to the number of int mp,fp;        char c;        memset (A,0,sizeof (a));            while (~ (C=getchar ()) &&c!= ' \ n ') {char name[2][50]= {"", ""};            Read (c,flag,name);                if (flag) {int P=check (flag,mp,fp,name,cnum);                if (p==-1) printf ("oops!\n");            else work (flag,p,mp,fp,name,cnum);    }} printf ("\ n"); } return 0;}
String processing
#include <iostream> #include <cstdio> #include <map> #include <string>using namespace Std;int    Main () {map<string,string>fa;    map<string,int>cla;    map<string,int>me;    String S,first,cname,m,cm,super;    int t;    cin>>t;        while (t) {cin>>s;            if (s== "Begin") {cla.clear ();            Fa.clear ();        Me.clear ();            } else if (s== "End") {t--;        cout<< "\ n";            } else if (s== "class") {cin>>first;            int I=first.find (': ');                if (I==string::npos) {if (Cla[first]) cout<< "oops!\n";                    else {cla[first]=1;                cout<<s<< "" <<first<< "\ n";                }} else {super=first.substr (i+1); First=first.eraSE (i);                    if (cla[super]==1 && cla[first]==0) {cla[first]=1;                    Fa[first]=super;                cout<<s<< "<<first<<": "<<super<<" \ n ";            } else cout<< "oops!\n";            }} else if (s== "Def") {cin>>first; Cname=first.substr (0,first.find ('. '));            /sub str before.            if (cla[cname]==0) cout<< "oops!\n";                else {if (me[first]==1) cout<< "redef" <<first<< "\ n";                    else {me[first]=1;                cout<<s<< "" <<first<< "\ n";            }}} and else if (s== "undef") {cin>>first;            if (me[first]==0) cout<< "oops!\n"; Else {me[first]=0;            cout<<s<< "" <<first<< "\ n";            }} else if (s== "call") {cin>>first;            M=first.substr (First.find ('. '));            Cname=first.substr (0,first.find ('. '));            Cm=first;                while (me[cm]==0 && (!cname.empty)) {cm=fa[cname]+m;            Cname=fa[cname];            } if (Cname.empty ()) cout<< "oops!\n";        else cout<< "invoke" <<cm<< "\ n"; }} return 0;}

"ZOJ 3480" Duck Typing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.