AOJ/數據結構習題集

来源:http://www.cnblogs.com/VincentValentine/archive/2017/04/28/6783551.html
-Advertisement-
Play Games

ALDS1_3_A/ALDS1_3_B/ALDS1_3_C/ALDS1_3_D ...


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;
}

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 事情是這樣的,我是linux菜鳥,然後在物理機上裝了centos6.5系統,然後新增加了一塊網卡,但是在圖形界面上看到這塊新增加的網卡顯示設備未托管的狀態, 然後在網上查了很多資料都是烏班圖的說是把什麼false改成true ,對我的情況不管用,上圖 如圖,第一塊網卡就是新增的網卡 然後ifconf ...
  • 1. 簡介 Aspose.words 可以在不使用 Microsoft.Word 的情況下生成、修改、轉換、列印文檔。不依賴office組件,這一點給我們提供了極大的便利性,可以簡單的引入 DLL(Dynamic Link Library,動態鏈接庫文件) ,就可以操作 word 文檔。不過也有一點 ...
  • 創建一個ASP.NET MVC項目。打開NuGet管理,安裝angularjs: 在App_Start目錄下,Bundle剛剛安裝的angularjs庫:在Global.asax.cs的Application_Start()方法,添加bundler。讓程式啟動時,即載入angularjs。 部署完成 ...
  • http://www.freejs.net/article_biaodan_278.html 這是在網上找到方法,我修改了一下實合我的項目,發博只為收藏記錄並加深記憶。 修改後效果如下 ...
  • 最近在搗鼓一個稍微有點low的商城網站,沒有計劃做app卻要求有個wap版,而前端又沒有做成響應式,時間WTF,直接利用了asp.net mvc的Display Mode Provider。 使用方式依照上面的鏈接地址,asp.net mvc application啟動的時候會在全局變數 Displ ...
  • 章節:“5w1h2k”分析法 what:我想知道某個“關鍵詞(keyword)”(即,辭彙、詞語,或稱單詞,可以是概念|專業術語|.......)的定義。 why:我想知道事物發生的原因。“why”代表的是一種“演繹推理”;我會不會犯“歸因錯誤”?是“單因素”的還是“多因素”的原因?是直接原因,還是 ...
  • 1、簡介 在現實生活中,比如你去市場買菜,在交完錢後你要求先去乾一些別的事情,稍候再來拿菜;如果這個時候某個陌生人要求把菜拿走,賣菜的人會把菜給陌生人嗎?!當然,這隻是一個比喻,但這恰恰就是會話劫持的喻意。所謂會話,就是兩台主機之間的一次通訊。例如你Telnet到某台主機,這就是一次Telnet會話 ...
  • E_S源碼百度雲分享鏈接: http://pan.baidu.com/s/1dFHzEJv 思維導圖源文件分享鏈接: http://pan.baidu.com/s/1hrAXGC8 簡單PPT分享鏈接: http://pan.baidu.com/s/1qYCZ1TE ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...