| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
WA all the time.who can tell me why?Plz#include <stdio.h>
#include <string.h>
#define HASH 20000
#define N 100
int hash[HASH];
char s[N];
int stack[N];
int top;
bool empty()
{
if(top>0) return false;
else return true;
}
void push(int n)
{
stack[top]=n;
top++;
}
int pop()
{
top--;
return stack[top];
}
void init()
{
memset(hash,0xff,sizeof(hash));
}
void get_first()
{
int i,m,l,k;
int sum;
while(1)
{
scanf("%s",s);
if(strcmp(s,"END_OF_FIRST_PART")==0) break;
scanf("%d",&m);
l=strlen(s);
sum=s[0]-'A';
k=26;
for(i=1;i<l;i++)
{
sum+=(s[i]-'a')*k;
k*=26;
}
hash[sum]=m;
}
}
//( -10 )
int count()
{
int i,j,k,m;
int len;
int tmp;
char du[N];
int dtop;
len=strlen(s);
dtop=0;
top=0;
i=0;
while(i<len)
{
if(s[i]=='(')
{
if(dtop>0)
{
m=0;
k=1;
for(j=0;j<dtop;j++)
{
m+=(du[j]-'a')*k;
k*=26;
}
dtop=0;
if(hash[m]==-1) return -1;
push(hash[m]);
}
push(-10);
i++;
}
else if(s[i]==')')
{
if(dtop>0)
{
m=0;
k=1;
for(j=0;j<dtop;j++)
{
m+=(du[j]-'a')*k;
k*=26;
}
dtop=0;
if(hash[m]==-1) return -1;
push(hash[m]);
}
int tt=pop();
tmp=0;
while(tt!=-10)
{
tmp+=tt;
tt=pop();
}
push(tmp);
i++;
}
else if(s[i]>='0' && s[i]<='9')
{
if(dtop>0)
{
m=0;
k=1;
for(j=0;j<dtop;j++)
{
m+=(du[j]-'a')*k;
k*=26;
}
dtop=0;
if(hash[m]==-1) return -1;
push(hash[m]);
}
tmp=pop();
if(i+1<len && s[i+1]>='0' && s[i+1]<='9')
{
tmp*=(s[i]-'0')*10+s[i+1]-'0';
push(tmp);
i+=2;
}
else
{
tmp*=s[i]-'0';
push(tmp);
i++;
}
}
else if(s[i]>='A' && s[i]<='Z')
{
if(dtop>0)
{
m=0;
k=1;
for(j=0;j<dtop;j++)
{
m+=(du[j]-'a')*k;
k*=26;
}
if(hash[m]==-1) return -1;
push(hash[m]);
dtop=0;
}
du[dtop]='a'+s[i]-'A';
dtop++;
i++;
}
else if(s[i]>='a' && s[i]<='z')
{
du[dtop]=s[i];
dtop++;
i++;
}
}
if(dtop>0)
{
m=0;
k=1;
for(j=0;j<dtop;j++)
{
m+=(du[j]-'a')*k;
k*=26;
}
if(hash[m]==-1) return -1;
push(hash[m]);
dtop=0;
}
tmp=0;
while(empty()==false)
{
int kk=pop();
tmp+=kk;
}
return tmp;
}
int main()
{
init();
get_first();
while(1)
{
scanf("%s",s);
if(strcmp(s,"0")==0) break;
int k=count();
if(k==-1) printf("UNKNOWN\n");
else printf("%d\n",k);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator