PAT - 栈、队列、链表

40 篇文章 0 订阅
35 篇文章 0 订阅

【Codeup 1918】简单计算器

  1. 数据结构node,用来存储操作数和操作符,flag来表示类型
  2. stack操作符栈【遇到相等或者高优先级操作符,弹出】;queue用来存储转换成的后缀表达式;map实现操作符优先级的映射。
  3. 使用(getline(cin, str), str!= "0"),来输入若干字符串str,str = "0",终止输入。
  4. 使用 string::iterator it = it.end(); 由于end的索引是最后一个元素的下一位,要 -1,才有元素,不然会报错。
#include<iostream>
#include<cstdio>
#include<string>
#include<stack>
#include<queue>
#include<map>
using namespace std;

struct node {
	double num;
	char op;
	bool flag;
};

string str;
stack<node> s;
queue<node> q;
map<char, int> op;

void Change() {
	double num;
	node temp;
	for (int i = 0; i < str.length();) {
		if (str[i] >= '0' && str[i] <= '9') {
			temp.flag = true;
			temp.num = str[i++] - '0';
			while (i < str.length() && str[i] >= '0' && str[i] <= '9') {
				temp.num = temp.num * 10 + (str[i] - '0');
				i++;
			}
			q.push(temp);
		}
		else {
			temp.flag = false;
			while (!s.empty() && op[str[i]] <= op[s.top().op]) {
				q.push(s.top());
				s.pop();
			}
			temp.op = str[i];
			s.push(temp);
			i++;

		}
	}
	while (!s.empty()) {
		q.push(s.top());
		s.pop();
	}

}

double Cal() {
	double temp1, temp2;
	node cur, temp;
	while (!q.empty()) {
		cur = q.front();
		q.pop();
		if (cur.flag == true) s.push(cur);
		else {
			temp2 = s.top().num;
			s.pop();
			temp1 = s.top().num;
			s.pop();
			temp.flag = true;
			if (cur.op == '+') temp.num = temp1 + temp2;
			else if (cur.op == '-') temp.num = temp1 - temp2;
			else if (cur.op == '*') temp.num = temp1 * temp2;
			else temp.num = temp1 / temp2;
			s.push(temp);
		}
	}
	return s.top().num;
}

int main() {
	op['+'] = op['-'] = 1;
	op['*'] = op['/'] = 2; //map
	while (getline(cin, str), str != "0") {
		for (string::iterator it = str.end() - 1; it !=str.begin(); it--) { //注意! -1
			if (*it == ' ') { str.erase(it); }
		}
		while (!s.empty()) s.pop(); // 清空栈
		Change();
		printf("%.2f\n", Cal());
	}

	return 0;
}

1032 Sharing (25 分)

To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, loading and being are stored as showed in Figure 1.

fig.jpg

Figure 1

You are supposed to find the starting position of the common suffix (e.g. the position of i in Figure 1).

Input Specification:

Each input file contains one test case. For each case, the first line contains two addresses of nodes and a positive N (≤10​5​​), where the two addresses are the addresses of the first nodes of the two words, and N is the total number of nodes. The address of a node is a 5-digit positive integer, and NULL is represented by −1.

Then N lines follow, each describes a node in the format:

Address Data Next

whereAddress is the position of the node, Data is the letter contained by this node which is an English letter chosen from { a-z, A-Z }, and Next is the position of the next node.

Output Specification:

For each case, simply output the 5-digit starting position of the common suffix. If the two words have no common suffix, output -1 instead.

Sample Input 1:

11111 22222 9
67890 i 00002
00010 a 12345
00003 g -1
12345 D 67890
00002 n 00003
22222 B 23456
11111 L 00001
23456 e 67890
00001 o 00010

Sample Output 1:

67890

Sample Input 2:

00001 00002 4
00001 a 10001
10001 s -1
00002 a 10002
10002 t -1

Sample Output 2:

-1

在最初的时候自己考虑,会不会出现相同节点的问题,最后发现多虑了,sharing point,共享节点,共用节点。

自己的思路是,使用struct存储data和next,然后search方法建两个vector,分别存储两个list的地址序列,最后逆序比较,当地址不同时跳出,输出最后一个相同的地址即可。

注意:char会读入空格,要先把空格的位置留出来。

出现了一个超时和一个错误,超时应该是vector太大了超出数组界限导致的,错误的原因还没找到。

#include<cstdio>
#include<vector>
using namespace std;

struct node {
	char data;
	//int pre;
	int next;
}Node[1000010];

int search(int head[]) {
	vector<int> list1, list2;
	int current[2];
	current[0] = head[0], current[1] = head[1];
	while ((Node[current[0]].next != -1) && (Node[current[1]].next != -1)) {
		list1.push_back(current[0]);
		list2.push_back(current[1]);
		current[0] = Node[current[0]].next;
		current[1] = Node[current[1]].next;
	}
	while(Node[current[0]].next != -1) {
		list1.push_back(current[0]);
		current[0] = Node[current[0]].next;
	}
	while (Node[current[1]].next != -1) {
		list2.push_back(current[1]);
		current[1] = Node[current[1]].next;
	}
	list1.push_back(current[0]);
	list2.push_back(current[1]);
	int start_pos = -1;
	vector<int>::iterator it1 = list1.end() - 1;
	vector<int>::iterator it2 = list2.end() - 1; //容易出错
	for (; it1 != list1.begin() && it2 != list2.begin(); it1--, it2--) {
		if (*it1 == *it2) {
			start_pos = *it1;
		}
		else {
			break; // jump out, if subfix ends
		}
	}
	return start_pos;


}


int main() {
	int head[2], N, tail[2];
	scanf("%d%d%d", & head[0], &head[1], &N);
	int j = 0;
	for (int i = 0; i < N; i++) {
		int tmp_addr;
		scanf("%d ", &tmp_addr);
		scanf("%c %d", &Node[tmp_addr].data, &Node[tmp_addr].next);
		if (Node[tmp_addr].next == -1){
			tail[j] = tmp_addr;
			j++;
		}
		else {
			//Node[Node[tmp_addr].next].pre = tmp_addr;
		}
	}
	int rst = search(head);
	if (rst != -1) {
		printf("%05d", rst);
	}
	else {
		printf("-1");
	}
	

	return 0;

}

 看了题解,发现自己想复杂了,其实不用每一个节点都check,题目默认的是找到开始节点就好了,只要找到公共开始节点,后面的节点都是共用。

怎么快速判断是否共用呢?最好的方法是加一个flag,然后对序列1在线处理,然后序列2逐个比较就可以了。

重构后的代码如下,可以全部通过

#include<cstdio>
#include<vector>
using namespace std;

const int maxn = 100010;

struct node {
	char data;
	int next;
	bool flag; //flag用来判断序列2中是否存在相同的数字
}Node[maxn];

int search(int head[]) {
	int ptr;
	for (ptr = head[0]; ptr != -1; ptr = Node[ptr].next)
		Node[ptr].flag = 1;
	for (ptr = head[1]; ptr != -1; ptr = Node[ptr].next)
	{
		if (Node[ptr].flag == 1)
			break;
	}

	return ptr;


}


int main() {
	for (int i = 0; i < maxn; i++) {
		Node[i].flag = 0;
	}
	int head[2], N;
	scanf("%d%d%d", & head[0], &head[1], &N);
	int j = 0;
	for (int i = 0; i < N; i++) {
		int tmp_addr;
		scanf("%d ", &tmp_addr);
		scanf("%c %d", &Node[tmp_addr].data, &Node[tmp_addr].next);
		
	}
	int rst = search(head);
	if (rst != -1) {
		printf("%05d", rst);
	}
	else {
		printf("%d", rst);
	}
	

	return 0;

}

 

 

1052 Linked List Sorting (25 分)

A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive N (<10​5​​) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by −1.

Then N lines follow, each describes a node in the format:

Address Key Next

where Address is the address of the node in memory, Key is an integer in [−10​5​​,10​5​​], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.

Output Specification:

For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.

Sample Input:

5 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345

Sample Output:

5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333
33333 100000 -1

从这道题中学到了很多点。

1.给出N个结点,但是不一定都有效,要从头结点开始,遍历一遍,判断哪个是有效结点。特判count = 0,在这个条件下,一个有效结点都没有,输入应该是"0 -1",输出"0 -1"。

2.虽然结点个数N是<105的,但是地址是一个5-digit number, maxn要到10W级别。

3.将数组排序的这个思想真的太神奇了!通过valid进行判断,将有效结点移动到前面!这样输出非常方便,但是注意要在struct里特别记录下结点地址,不然排序后会丢失。

4.注意输出addr的时候要用%05d补全,但是最后一个结点要输出-1.

 

#include<cstdio>
#include<algorithm>
using namespace std;

const int maxn = 100010;

struct node {
	int addr, data, next;
	bool valid;
}Node[maxn];

bool cmp(node a, node b) {
	if (a.valid == 0 || b.valid == 0) {
		return a.valid > b.valid; //假如有无效的,后移
	}
	else {//假如两个数据都有效,按data从小到大排序
		return a.data < b.data;
	}

}

int main() {
	int N, start_addr;
	for (int i = 0; i < maxn; i++)
		Node[i].valid = false;
	scanf("%d%d", &N, &start_addr);


	for (int i = 0; i < N; i++) {
		int addr, data, next;
		scanf("%d%d%d", &addr, &data, &next);
		Node[addr].addr = addr;
		Node[addr].data = data;
		Node[addr].next = next;
		//Node[addr].valid = true;
	}
	int ptr = start_addr, count = 0;

	while (ptr != -1) {
		Node[ptr].valid = true;
		ptr = Node[ptr].next;
		count++;
	}

	if (count == 0) {
		printf("0 -1");
		return 0;
	}

	sort(Node, Node + maxn, cmp);

/*	for (int i = 0; i < N; i++)
	{
		if (i == N - 1)
		{
			Node[i].next = -1;
		}
		else {
			Node[i].next = Node[i + 1].addr;
		}
	}
*/
	printf("%d %05d\n", count, Node[0].addr); //注意这里是count不是N
	for (int i = 0; i < count; i++) {
		if (i != count - 1)
			printf("%05d %d %05d\n", Node[i].addr, Node[i].data, Node[i+1].addr);
		else
			printf("%05d %d -1\n", Node[i].addr, Node[i].data);
	}


	return 0;
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值