ALDS1_3_A-Stack.
Description:

Write a program which reads an expression in the Reverse Polish notation and prints the computational result.

An expression in the Reverse Polish notation is calculated using a stack. To evaluate the expression, the program should read symbols in order. If the symbol is an operand, the corresponding value should be pushed into the stack. On the other hand, if the symbols is an operator, the program should pop two elements from the stack, perform the corresponding operations, then push the result in to the stack. The program should repeat this operations.

Input:

An expression is given in a line. Two consequtive symbols (operand or operator) are separated by a space character.

You can assume that +, - and * are given as the operator and an operand is a positive integer less than 106

Output:

Print the computational result in a line.

Constraints:

2 ≤ the number of operands in the expression ≤ 100

1 ≤ the number of operators in the expression ≤ 99

-1 × 109 ≤ values in the stack ≤ 109

Sample Input 1:

1 2 +

Sample Output 1:

3

Sample Input 2:

1 2 + 3 4 - *

Sample Output 2:

-3

Codes:
//#define LOCAL

#include <cstdio>
#include <cstdlib> #define maxSize 1000
char s[maxSize];
int top, S[maxSize]; void push(int x) {
S[++top] = x;
} int pop() {
--top;
return S[top+1];
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int a, b;
while(scanf("%s", s) != EOF) {
if(s[0] == '+') {
a = pop(); b = pop();
push(a+b);
} else if(s[0] == '-') {
b = pop(); a = pop();
push(a-b);
} else if(s[0] == '*') {
a = pop(); b = pop();
push(a*b);
} else push(atoi(s));
} printf("%d\n", pop()); return 0;
}
ALDS1_3_B-Queue.
Description:

For example, we have the following queue with the quantum of 100ms.

A(150) - B(80) - C(200) - D(200)

First, process A is handled for 100ms, then the process is moved to the end of the queue with the remaining time (50ms).

B(80) - C(200) - D(200) - A(50)

Next, process B is handled for 80ms. The process is completed with the time stamp of 180ms and removed from the queue.

C(200) - D(200) - A(50)

Your task is to write a program which simulates the round-robin scheduling .

Input:

n q

name1 time1

name2 time2

...

namen timen

In the first line the number of processes n and the quantum q are given separated by a single space.

In the following n lines, names and times for the n processes are given. namei and timei are separated by a single space.

Output:

For each process, prints its name and the time the process finished in order.

Constraints:

1 ≤ n ≤ 100000

1 ≤ q ≤ 1000

1 ≤ timei ≤ 50000

1 ≤ length of namei ≤ 10

1 ≤ Sum of timei ≤ 1000000

Sample Input:

5 100

p1 150

p2 80

p3 200

p4 350

p5 20

Sample Output:

p2 180

p5 400

p1 450

p3 550

p4 800

Codes:
//#define LOCAL

#include <cstdio>

int head, tail;
#define len 100010
typedef struct pp {
char name[20];
int time;
} P;
P Q[len]; void enqueue(P x) {
Q[tail] = x;
tail = (tail+1)%len;
}
P dequeue() {
P x = Q[head];
head = (head+1)%len;
return x;
}
int min(int a, int b) {return a<b?a:b;} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int n, q, last = 0;
scanf("%d%d", &n, &q);
for(int i=0; i<n; ++i)
scanf("%s%d", Q[i].name, &Q[i].time); head = 0, tail = n;
while(head != tail) {
P u = dequeue();
int x = min(q, u.time);
u.time -= x; last += x;
if(u.time) enqueue(u);
else printf("%s %d\n", u.name, last);
} return 0;
}
ALDS1_3_C-DoublyLinkedList.
Description:

Your task is to implement a double linked list.

Write a program which performs the following operations:

insert x: insert an element with key x into the front of the list.

delete x: delete the first element which has the key of x from the list. If there is not such element, you need not do anything.

deleteFirst: delete the first element from the list.

deleteLast: delete the last element from the list.

Input:

The input is given in the following format:

n

command1

command2

...

commandn

In the first line, the number of operations n is given. In the following n lines, the above mentioned operations are given in the following format:

insert x

delete x

deleteFirst

deleteLast

Output:

Print all the element (key) in the list after the given operations. Two consequtive keys should be separated by a single space.

Constraints:

The number of operations ≤ 2,000,000

The number of delete operations ≤ 20

0 ≤ value of a key ≤ 109

The number of elements in the list does not exceed 106

For a delete, deleteFirst or deleteLast operation, there is at least one element in the list.

Sample Input 1:

7

insert 5

insert 2

insert 3

insert 1

delete 3

insert 6

delete 5

Sample Output 1:

6 1 2

Sample Input 2:

9

insert 5

insert 2

insert 3

insert 1

delete 3

insert 6

delete 5

deleteFirst

deleteLast

Sample Output 2:

1

Codes:
//#define LOCAL

#include <cstdio>
#include <cstring>
#include <cstdlib> struct Node{
int key;
Node *prev, *next;
};
Node *nil; Node* listSearch(int key) {
Node *cur = nil->next;
while(cur!=nil && cur->key!=key) cur = cur->next;
return cur;
} void init() {
nil = (Node *)malloc(sizeof(Node));
nil->next = nil, nil->prev = nil;
} void printList() {
Node *cur = nil->next;
int isf = 0;
while(1) {
if(cur == nil) break;
if(isf++ > 0) printf(" ");
printf("%d", cur->key);
cur = cur->next;
}
printf("\n");
} void deletNode(Node *t) {
if(t == nil) return;
t->prev->next = t->next;
t->next->prev = t->prev;
free(t);
} void deleteFirst() {deletNode(nil->next);}
void deleteLast() {deletNode(nil->prev);}
void deleteKey(int key) {deletNode(listSearch(key));} void insert(int key) {
Node *x = (Node *)malloc(sizeof(Node));
x->key = key;
x->next = nil->next, nil->next->prev = x;
nil->next = x, x->prev = nil;
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif char com[20];
int key, n, i, size, np, nd;
size = np = nd = 0;
scanf("%d", &n); init();
for(i=0; i<n; ++i) {
scanf("%s%d", com, &key);
int len = strlen(com);
if(com[0] == 'i') {
insert(key);
++np, ++size;
} else if(com[0] == 'd') {
if(len > 6) {
if(com[6] == 'F') deleteFirst();
else if(com[6] == 'L') deleteLast();
} else {
deleteKey(key); ++nd;
}
--size;
}
}
printList(); return 0;
}
ALDS1_3_D-AreasOnTheCross-SectionDiagram.
Codes:
#include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <algorithm>
using namespace std; int main()
{
stack<int> S1;
stack<pair<int, int> > S2;
char ch; int sum = 0;
for(int i=0; cin>>ch; ++i) {
if(ch == '\\') S1.push(i);
else if(ch=='/' && S1.size()>0) {
int j = S1.top(); S1.pop();
sum += i-j; int a = i-j;
while(S2.size()>0 && S2.top().first>j) {
a += S2.top().second; S2.pop();
}
S2.push(make_pair(j, a));
}
} vector<int> ans;
while(S2.size() > 0) {
ans.push_back(S2.top().second); S2.pop();
}
reverse(ans.begin(), ans.end());
cout << sum << endl << ans.size();
for(int i=0; i<ans.size(); ++i) cout << " " << ans[i];
cout << endl; return 0;
}

AOJ/数据结构习题集的更多相关文章

  1. PTA数据结构习题集

    https://blog.csdn.net/qq_43733499/category_8956159.html https://www.cnblogs.com/nonlinearthink/tag/% ...

  2. 一种很有意思的数据结构:Bitmap

    昨晚遇到了一种很有意思的数据结构,Bitmap. Bitmap,准确来说是基于位的映射.其中每个元素均为布尔型(0 or 1),初始均为 false(0).位图可以动态地表示由一组无符号整数构成的集合 ...

  3. PAT mooc DataStructure 4-2 SetCollection

    数据结构习题集-4-2 集合的运用 1.题目: We have a network of computers and a list of bi-directional connections. Eac ...

  4. 《数据结构-C语言版》(严蔚敏,吴伟民版)课本源码+习题集解析使用说明

    <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题集解析使用说明 先附上文档归类目录: 课本源码合辑  链接☛☛☛ <数据结构>课本源码合辑 习题集全解析  链接☛☛☛  ...

  5. AOJ/初等排序习题集

    ALDS1_1_D-MaximumProfit. Codes: //#define LOCAL #include <cstdio> #include <algorithm> u ...

  6. AOJ/搜索递归分治法习题集

    ALDS1_4_A-LinearSearch. Description: You are given a sequence of n integers S and a sequence of diff ...

  7. AOJ/树二叉搜索树习题集

    ALDS1_7_A-RootedTree. Description: A graph G = (V, E) is a data structure where V is a finite set of ...

  8. AOJ/堆与动态规划习题集

    ALDS1_9_A-CompleteBinaryTree. Codes: //#define LOCAL #include <cstdio> int parent(int i) { ret ...

  9. PAT-中国大学MOOC-陈越、何钦铭-数据结构基础习题集 00-自測4. Have Fun with Numbers (20) 【二星级】

    题目链接:http://www.patest.cn/contests/mooc-ds/00-%E8%87%AA%E6%B5%8B4 题面: 00-自測4. Have Fun with Numbers ...

随机推荐

  1. VS2010在网络共享目录使用IntelliSense、ipch、sdf和SQL Compact Server相关问题

    Microsoft SQL Compact Server 是专用于 Visual Studio 的单机SQL 数据库.数据库文件名的后缀为SDF. 而VS2010 拒绝在网络共享目录中建立和打开SDF ...

  2. Druid连接池配置(java无框架)

    连接池是一个对数据库连接进行管理的东西,当一个线程需要用 JDBC 对 数据库操作时,它从池中请求一个连接.当这个线程使用完了这个连接,将它返回到连接池中,这样这就可以被其它想使用它的线程使用,而不是 ...

  3. Ext JS 6学习文档–第1章–ExtJS入门指南

    Ext JS 入门指南 前言 本来我是打算自己写一个系列的 ExtJS 6 学习笔记的,因为 ExtJS 6 目前的中文学习资料还很少.google 搜索资料时找到了一本国外牛人写的关于 ExtJS ...

  4. 通过hue提交oozie定时任务

    Oozie是什么? Oozie是一种Java Web应用程序,它运行在Java servlet容器——即Tomcat——中,并使用数据库来存储以下内容: 工作流定义 当前运行的工作流实例,包括实例的状 ...

  5. vpn的实现原理

    由于公共IP的短缺,我们在组建局域网时,通常使用保留地址作为内部IP,(比如最常用的C类保留地址:192.168.0.0-192.168.255.255)这些地址是不会被互联网分配的,因此它们在互联网 ...

  6. SQL AlawaysOn 之二:添加组织和域用户

    1.在管理工具打开Active Directory 用户和计算机 2.在域控制器名称下面右键  选择 新建--组织单位, 3.输入组织名定,点确定 4.在组织右键--新建--用户 5.输入用户信息,点 ...

  7. 自定义template

    今天写代码写的有点烦了,感觉天天写new String(); new HashMap<String,String>()等,感觉写烦了,有没有快速的方法了.就你输入syso然后按atl+/就 ...

  8. WPF之路四:窗体自适应

    下面我来举个例子说明如何用Grid或DockPanel来实现自适应窗体. 让我们新建一个WPF工程,完成后我们打开对应的XAML文件,可以看到VS已经自动添加了<Grid></Gri ...

  9. 自动化测试培训:设计和实现分布式QTP调用

    自动化测试培训:设计和实现分布式QTP调用   自动化测试的过程中一个很核心的需求就是执行效率,单位时间里要执行更多的测试用例.为了完成该要求,我们开发一个调度工具,让qtp运行在不同的机器上,通过C ...

  10. 《ECMAScript标准入门》第二版读书笔记

    title: <ECMAScript标准入门>第二版 date: 2017-04-10 tags: JavaScript categories: Reading-note 2015年6月, ...