UVA 算法竞赛 入门经典 Volume 2. Data Structures :: Lists 题解合集

点击打开链接

UVA 442 Matrix Chain Multiplication

#include<cstdio>
#include<iostream>
#include <list>
#include <vector>
#include <stack>
#include <cstring>
using namespace std;

struct node {
    int a;
    int b;
};

int multi(node a, node b) {
    if (a.b == b.a)
        return a.a * a.b * b.b;
    else return -1;
}

int main() {
    freopen("input.txt", "r", stdin);
    int t;
    node letter[27];
    char xxx[100];
    scanf("%d%*c", &t);
    for (int i = 0; i < t; i++)
        scanf("%*c %d %d%*c", &letter[i].a, &letter[i].b);
    while (gets(xxx)) {
        int sum = 0;
        node temp = {0, 0};
        bool submark = 0,doitnow=0;
        stack< node >  stack1,stack2;
        for (int i = strlen(xxx) - 1; i >= 0; i--) {
            if (xxx[i] == ')') {
                submark=true;
                if(doitnow==true){
                    doitnow=false;
                    stack2.push(temp);
                }
            }
            else if(submark==true){
                stack1.push(letter[xxx[i]-'A']);
                submark=false;
            }
            else if(isalpha(xxx[i])){
                if(doitnow==true){
                     if (multi(letter[xxx[i]-'A'],temp) == -1) {
                        printf("error\n");
                        goto done;
                    } else {
                        sum += multi(letter[xxx[i]-'A'],temp);
                        temp.a = letter[xxx[i]-'A'].a;
                    }
                    doitnow=false;
                    continue;
                }
                temp = letter[xxx[i] - 'A'];
                while (!stack1.empty()) {
                    if (multi(temp, stack1.top()) == -1) {
                        printf("error\n");
                        goto done;
                    } else {
                        sum += multi(temp, stack1.top());
                        temp.b = stack1.top().b;
                        stack1.pop();
                    }
                }
            }
            else{
                doitnow=true;
            }
        }
        while (!stack2.empty()) {
            if (multi(temp, stack2.top()) == -1) {
                printf("error\n");
                goto done;
            } else {
                sum += multi(temp, stack2.top());
                temp.b = stack2.top().b;
                stack2.pop();
            }
        }
        printf("%d\n",sum);
        done:;
    }
    return 0;
}

 

UVA 11111 Generalized Matrioshkas

#include<cstdio>
#include<iostream>
#include <list>
#include <vector>
#include <stack>
#include <cstring>
using namespace std;
struct node{
	int a;
	int val;
};
stack<node> st;
int main() {
	freopen("input.txt", "r", stdin);
	int aaa;
	char letter;
	bool shibai=0;
	while(scanf("%d%c",&aaa,&letter)==2){
		node temp;
		temp.a=aaa;
		temp.val=-aaa;
		if(aaa<0){
			if(st.empty())
				st.push(temp);
			else{
				if(st.top().val<=(-aaa))
					shibai=true;
				st.top().val+=aaa;
				st.push(temp);
			}
		}
		else{
			if(st.empty()||(aaa+st.top().a)!=0)
				shibai=true;
			else
				st.pop();
		}
		if(letter=='\n'){
			if(shibai==true||!st.empty()){
				printf(":-( Try again.\n");
				while(!st.empty())
					st.pop();
				shibai=false;
			}
			else
				printf(":-) Matrioshka!\n");
		}
	}
	return 0;
}

 

UVA 11234 Expressions

#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#include <list>
#include <algorithm>
#include <queue>
using namespace std;
struct node{
	char val;
	node *left;
	node *right;
};
queue<node*>  que;
stack<node*>  st;
int main()
{
	freopen("input.txt","r",stdin);
	int t,len;
	node* temp;
	char str[10001];
	scanf("%d%*c",&t);
	while(t--){
		gets(str);
		len=strlen(str);
		for(int i=0;i<len;i++)
		{
			if(islower(str[i])){
				temp=new node;
				temp->val=str[i];
				temp->right=temp->left=NULL;
				st.push(temp);
			}
			else{
				temp=new node;
				temp->val=str[i];
				temp->right=st.top();
				st.pop();
				temp->left=st.top();
				st.pop();
				st.push(temp);
			}
		}
		int i=0;
		node* cur=st.top();
		que.push(cur);
		while(!que.empty()){
			cur=que.front();
			que.pop();
			if(cur->left)
				que.push(cur->left);
			if(cur->right)
				que.push(cur->right);
			str[i++]=cur->val;
		}
		for(int i=len-1;i>=0;i--)
			printf("%c",str[i]);
		printf("\n");
		while(!st.empty())
			st.pop();
	}
	return 0;
}


 

UVA 540 Team Queue

#include<cstdio>
#include<iostream>
#include <list>
#include <vector>
#include <stack>
#include <cstring>
#include <algorithm>
using namespace std;
int t;
vector<int> teams[1001];
struct node{
	int mem;
	int team;
};
list<node> que;
int findteam[1000001];
int main() {
	freopen("input.txt", "r", stdin);
	int cas=1;
	list<node>::iterator  teampos[1001];
	while (scanf("%d%*c", &t), t) {
		que.clear();
		for(int i=0;i<t;i++)
			teampos[i]=que.end();
		printf("Scenario #%d\n",cas++);
		for (int i = 0; i < t; i++) {
			vector<int> temp;
			int teammate;
			scanf("%d%*c", &teammate);
			while (teammate--) {
				int menber;
				scanf("%d%*c", &menber);
				temp.push_back(menber);
				findteam[menber]=i;
			}
			teams[i] = temp;
		}
		char command[20];
		while(scanf("%s%*c",command),command[0]!='S'){
			if(command[0]=='E'){
				node enqmem;
				scanf("%d%*c",&enqmem.mem);
				enqmem.team=findteam[enqmem.mem];
				if(teampos[enqmem.team]==que.end()){
					que.push_back(enqmem);
					teampos[enqmem.team]=--que.end();
				}
				else{
					que.insert(++teampos[enqmem.team],enqmem);
					teampos[enqmem.team]--;
				}
			}
			else{
				for(int i=0;i<t;i++)
					if(teampos[i]==que.begin())
						teampos[i]=que.end();
				printf("%d\n",que.front().mem);
				que.pop_front();
			}
		}
		printf("\n");
	}
	return 0;
}


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值