poj 2246 递归 zoj 1094

#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

时间: 2024-11-03 21:31:44

poj 2246 递归 zoj 1094的相关文章

POJ 1011 递归 &amp; 回溯

题目大意:给出一些长度不大于 50 的木棍, 要求你把这些小木棍拼成*             长度相同木棍,当然长度越小越好.* 解题思路:这个题最近做了很多次,我比较有发言权了.*             思想很简单,一个接一个的把木棍拼起来,最后把木棍用光.*             关键的地方是几个剪枝技巧:*                   设所有木棍的总长度为 Sum, 最终的答案是 L. *             1. 首先要明白, Sum一定要能被 L 整除. *     

POJ 3652 &amp;amp; ZOJ 2934 &amp;amp; HDU 2721 Persistent Bits(数学 元)

主题链接: PKU:http://poj.org/problem?id=3652 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=1933 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2721 Description WhatNext Software creates sequence generators that they hope will produce fai

POJ 3653 &amp;amp; ZOJ 2935 &amp;amp; HDU 2722 Here We Go(relians) Again(最短路dijstra)

题目链接: PKU:http://poj.org/problem? id=3653 ZJU:problemId=1934" target="_blank">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1934 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=2722 Description The Gorelians are a warlike rac

POJ 1861 &amp;amp; ZOJ 1542 Network(最小生成树之Krusal)

题目链接: PKU:http://poj.org/problem?id=1861 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=542 Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the c

POJ 2246 Matrix Chain Multiplication

用栈来处理一下就好了. #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<map> using namespace std; struct Mar { int r,c; } mar[3000000]; int n; char s[100000]; int r,c,top,flag; Mar Stack[1000000]; int m

poj 1163 递归

#include <iostream> #include <stdlib.h> //use recurrence way to find most biggest total using namespace std; int sum=0; void print_value(int a[][110],int N) //printf the number { for(int row=1; row<=N; row++) for(int line=1; line<=row; l

poj 1094 Sorting It All Out[ topo]

传送门:http://poj.org/problem?id=1094 Sorting It All Out Description An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequen

【POJ】1094 Sorting It All Out(拓扑排序)

http://poj.org/problem?id=1094 原来拓扑序可以这样做,原来一直sb的用白书上说的dfs............ 拓扑序只要每次将入度为0的点加入栈,然后每次拓展维护入度即可.. 我是个大sb,这种水题调了一早上.. #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include &

poj 1094 Sorting It All Out 解题报告

题目链接:http://poj.org/problem?id=1094 题目意思:给出 n 个待排序的字母 和 m 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具体的字母顺序不能确定但至少不矛盾.这些关系均是这样的一种形式: 字母1 < 字母2 这道题目属于图论上的拓扑排序,由于要知道读入第几行可以确定具体的顺序,所以每次读入都需要进行拓扑排序来检验,这时每个点的入度就需要存储起来,所以就有了代码中memcpy 的使用了. 拓扑排序的思路很容易理解,但写起来