codeforces 7E

#include<cstring>
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<cstdio>
#include<map>
using namespace std;
map<string,int>mp;
string s;
char fin[1005];
int res[1005];
char store[105][2][1005];
int n;
void read(string s,int num)
{
    int l=s.length(),l0=0,l1=0,i=0;
    while (++i)
    {
        if (s[i]=='d')
        {
            i+=6;
            break;
        }
    }
    while (s[i]==' ')i++;
//    printf ("%d---\n",i);
    for (i; i<l; i++)
    {
        if (s[i]==' ')
        {
            store[num][0][l0++]='\0';
            break;
        }
        store[num][0][l0++]=s[i];
    }
    while (s[i]==' ') i++;
    for (i; i<l; i++)
    {
        if (s[i]!=' ')
            store[num][1][l1++]=s[i];
    }
    store[num][1][l1++]='\0';
//    cout<<store[num][0]<<endl;
//    cout<<store[num][1]<<endl;
}
int work(char* tmp,int l,int r)
{
    int lastl=0,op=0;
    for (int i=r;i>=l;i--)
    {
        if (tmp[i]=='(') op--;
        else if (tmp[i]==')') op++;
        else if (!op)
        {
            if ((tmp[i]=='*'||tmp[i]=='/')&&!lastl) lastl=i;
            else if (tmp[i]=='+')
            {
                int x=work(tmp,l,i-1),y=work(tmp,i+1,r);
                if (x==2||y==2) return 2;
                return 3;
            }
            else if (tmp[i]=='-')
            {
                int x=work(tmp,l,i-1),y=work(tmp,i+1,r);
                if (x==2||y==2||y==3) return 2;
                return 3;
            }
        }
    }
//    printf ("%s %d %d\n",tmp,l,r);
    if (lastl&&tmp[lastl]=='*')//全是乘除法
    {
        int i=lastl;
        int x=work(tmp,l,i-1),y=work(tmp,i+1,r);
        if (x==2||y==2||y==3||x==3) return 2;
        return 4;
    }
    else if (lastl&&tmp[lastl]=='/')
    {
        int i=lastl;
        int x=work(tmp,l,i-1),y=work(tmp,i+1,r);
        if (x==2||y==2||y==3||x==3||y==4) return 2;
        return 4;
    }
    else if (tmp[l]=='('&&tmp[r]==')')
    {
        if (work(tmp,l+1,r-1)==2) return 2;
        return 1;
    }
    else//单词
    {
        string x;
        for(int i=l; i<=r; i++)x+=tmp[i];
        if (mp[x]==0) return 1;
        else
        {
            if (res[mp[x]]!=0) return res[mp[x]];
            else
            {
                res[mp[x]]=work(store[mp[x]][1],0,strlen(store[mp[x]][1])-1);
                return res[mp[x]];
            }
        }
    }
}
int main()
{
    scanf ("%d",&n);
    getline(cin,s);
    for (int i=1; i<=n; i++)
    {
        getline(cin,s);
        read(s,i);
        mp[store[i][0]]=i;
    }
    getline(cin,s);
    int l=s.length(),st=0;
    for (int i=0;i<l;i++)
    {
        if (s[i]!=' ')
            fin[st++]=s[i];
    }
    fin[st++]='\0';
    if (work(fin,0,strlen(fin)-1)==2) printf ("Suspicious\n");
    else printf ("OK\n");
    return 0;
}

 

posted on 2016-10-14 14:19  very_czy  阅读(198)  评论(0编辑  收藏  举报

导航