算法竞赛入门经典第六章

来源:互联网 发布:淘宝怎么看真假 编辑:程序博客网 时间:2024/05/21 10:58

6.1.1 卡片游戏

C++ STL 队列的相关操作

入队  q.push(x)
出队  q.pop()     //出队列操作不返回该元素
访问队首  q.front()
访问队尾  q.back()
判断队列空  q.empty()
队列中的元素个数  q.size()


#include<cstdio>#include<queue>using namespace std;queue<int> q;int main(){    int n, i;    scanf("%d",&n);    for(i = 0; i < n; i++)        q.push(i+1);    while(!q.empty())    {        printf("%d ", q.front());        q.pop();        q.push(q.front());        q.pop();    }    return 0;}

6.1.2 铁轨

C++ STL 栈的相关操作

入栈  s.push(x)
出栈  s.pop()     //出栈操作不返回该元素
访问栈顶  s.top()
判断栈空  s.empty()
栈中的元素个数  s.size()


#include<cstdio>#include<stack>using namespace std;int main(){    int n, target[100], ok, i;    stack<int> sta1, sta2;    while(scanf("%d", &n) == 1)    {        ok = 1;        for(i = n; i > 0; i--)            sta2.push(i);        for(i = 0; i < n; i++)            scanf("%d", &target[i]);        for(i = 0; i < n; i++)        {            while(sta1.empty()||target[i] != sta1.top())            {                if(sta2.empty()) {ok = 0; break;}                sta1.push(sta2.top());                sta2.pop();            }            if(ok == 0) break;            else{sta1.pop(); continue;}        }        printf("%s\n", ok? "Yes" : "No" );    }    return 0;}


6.3.2 层次遍历

#include<stdio.h>#include<string.h>typedef struct TNode{    int have_value;    int val;    struct TNode* left;    struct TNode* right;}Node;Node* root;int fail;char s[100];int ans[400],n;Node* newnode(){    Node* a = (Node*)malloc(sizeof(Node));    if(a != NULL)    {        a->have_value = 0;        a->left = NULL;        a->right = NULL;    }    return a;}void addnode(int v, char *s){    Node* u = root;    int n = strlen(s), i;    for(i = 0; i < n; i++)    {        if(s[i] == 'R')        {            if(u->right == NULL) u->right = newnode();            u = u->right;        }        if(s[i] == 'L')        {            if(u->left == NULL) u->left = newnode();            u = u -> left;        }    }    if(u->have_value) fail = 1;    u->have_value = 1;    u->val = v;}void remove_tree(Node* u){    if(u == NULL) return;    remove_tree(u->left);    remove_tree(u->right);    free(u);}int readinput(){    fail = 0;    remove_tree(root);    root = newnode();    for(;;)    {        if(scanf("%s",s) != 1) return 0;        if(!strcmp(s,"()")) break;        int v;        sscanf(&s[1], "%d", &v);        addnode(v, strchr(s, ',') + 1);    }    return 1;}int bfs(){    int front = 0, rear = 1;    Node* q[400];    q[0] = root;    while(front < rear)    {        Node* u = q[front++];        if(!u->have_value) return 0;        ans[n++] = u -> val;        if(u->left != NULL) q[rear++] = u->left;        if(u->right != NULL) q[rear++] = u->right;    }    return 1;}int main(){    while(readinput())    {        n = 0;        if(bfs())        {            int i;            for(i = 0; i < n; i++)                printf("%d ", ans[i]);            printf("\n");        }        else printf("-1\n");    }    return 0;}


6.3.3 二叉树重建

给出先序,中序构建二叉树,再输出。

#include<stdio.h>#include<string.h>typedef struct TNode{    char val;    struct TNode* left;    struct TNode* right;}Node;Node* root;Node* build(char* pre, char* in, int n){    if(n <= 0) return NULL;    Node* u = (Node*)malloc(sizeof(Node));    u->val = pre[0];    int p = strchr(in, pre[0]) - in;    u->left = build(pre+1, in, p);    u->right = build(pre+p+1, in+p+1, n-p-1);    return u;}void postorder(Node* rt){    if(rt == NULL) return;    postorder(rt->left);    postorder(rt->right);    printf("%c",rt->val);}void freetree(Node* rt){    if(rt == NULL) return;    freetree(rt->left);    freetree(rt->right);    free(rt);}int main(){    char s1[100],s2[100];    while(scanf("%s%s",s1,s2)==2)    {        freetree(root);        root = build(s1, s2, strlen(s1));        postorder(root);        printf("\n");    }    return 0;}


再贴LEETCODE 一道相关题目  

Construct Binary Tree from Preorder and Inorder Traversal

 

/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     struct TreeNode *left; *     struct TreeNode *right; * }; */struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize) {    if(preorderSize <= 0) return NULL;    struct TreeNode* u = (struct TreeNode*)malloc(sizeof(struct TreeNode));    u->val = preorder[0];    int p = 0;    while(inorder[p] != preorder[0])        p++;    u->left = buildTree(preorder+1, p, inorder, p);    u->right = buildTree(preorder+p+1, preorderSize-p-1, inorder+p+1, preorderSize-p-1);    return u;}




0 0