poj 2246 递归 zoj 1094

时间:2014-08-10 21:10:11   收藏:0   阅读:345

 

bubuko.com,布布扣

 

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <stack>
using namespace std;
struct node
{
int m,n;
// bool f;
};
node hash[200];
char s[1000];
int main()
{
int i,n,sum;
char c;
bool b;
scanf("%d",&n);
while(n--)
{
getchar();
scanf("%c",&c);
scanf("%d%d",&hash[c].m,&hash[c].n);
}
node temp,temp1;
while(scanf("%s",s)!=EOF)
{ sum=0;b=1;
stack<char> s1;
stack<node> s2;
for(i=0;s[i]!=‘\0‘;i++)
{
if(s[i]==‘(‘)
s1.push(s[i]);
else if(s[i]==‘)‘)
{
c=s1.top();
s1.pop();
s1.pop();
while(!s1.empty()&&s1.top()!=‘(‘)
{
temp=s2.top();
s2.pop();
temp1=s2.top();
if(temp1.n!=temp.m)
{
b=0;break;
}
sum+=temp1.m*temp1.n*temp.n;
temp1.n=temp.n;
s2.pop();
s2.push(temp1);
s1.pop();
}
s1.push(c);
}
else
{
if(!s1.empty()&&s1.top()!=‘(‘)
{
temp=s2.top();
if(temp.n!=hash[s[i]].m)
{
b=0;break;
}
sum+=temp.m*temp.n*hash[s[i]].n;
temp.n=hash[s[i]].n;
s2.pop();
s2.push(temp);
}
else
{
s2.push(hash[s[i]]);
s1.push(‘#‘);
}
}
}
if(b)
printf("%d\n",sum);
else
printf("error\n");
}
return 0;
}

 

 

*************************************

 


//hnldyhy(303882171) 10:20:27
//POJ 2246(ZOJ 1094)                                                 堆栈

#include <iostream>
#include <stack>
#define MAX 100
using namespace std;
struct node
{ int row;
int col;
} mat[27];
stack <node> my;

int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
getchar(); // 9 后面的换行
int i;
char ch;
for(i = 1; i <= n; i++)
{
scanf("%c", &ch);
int id = ch - ‘A‘;
scanf("%d%d", &mat[id].row, &mat[id].col);
getchar();
}
char exp[110];
while(scanf("%s", &exp) != EOF)
{
getchar(); //换行
int len = strlen(exp);
while (!my.empty()) my.pop();
int total = 0;
bool flag = true;
int pos;
for( pos = 0; pos < len; pos++)
{
if(exp[pos] == ‘)‘)
{
node t1, t2,t;
t1 = my.top();
my.pop();
t2 = my.top();
my.pop();
t.row = t2.row;
t.col = t1.col;
if(t2.col != t1.row)
{ flag = false;
break;
}
total += t2.row * t2.col * t1.col;
my.push(t);
}
else if(exp[pos] != ‘(‘)
{
int id = exp[pos] - ‘A‘;
my.push(mat[id]);
}
}
if(flag == false)
printf("error\n");
else
printf("%d\n", total);
}
}
}

 


************************************************************************
// 递归

#include <iostream>
#define MAX 100
using namespace std;
struct node
{ int r;
int c;
int s;
} ;

int row[27],col[27];
char exp[110];
int pos;
bool flag;

struct node f()
{
struct node t,t1,t2;
if (exp[pos]==‘(‘)
{ pos++; t1=f(); t2=f();
pos++;
if (t1.c!=t2.r) flag=false;
t.r = t1.r; t.c = t2.c;
t.s=t1.s+t2.s+t1.r*t1.c*t2.c;
}
else if (exp[pos]!=‘)‘)
{ t.r=row[exp[pos]-‘A‘];
t.c=col[exp[pos]-‘A‘];
t.s=0;
pos++;
}
return t;
}

int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
getchar();
int i;
char ch;
for(i = 1; i <= n; i++)
{
scanf("%c", &ch);
int id = ch - ‘A‘;
scanf("%d%d", &row[id], &col[id]);
getchar();
}
while(scanf("%s", &exp) != EOF)
{
getchar();
pos=0;
flag=true;
struct node t=f();
if(flag == false)
cout<<"error\n";
else
cout<<t.s<<endl;
}
}
}

 

poj 2246 递归 zoj 1094,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!