爬格子呀6-1、6-2、6-3

告别了c++的熟悉,迈到了数据结构的大门;
对树的定义还不是那么的熟悉,只有不停的练习了;
希望好运;
代码如下:
6-1:

#include<cstdio>
#include<iostream>
#include<stack>
#include<string>
#include<sstream>

using namespace std;


int check(string s) {
    stack<string>a;
    stringstream mid(s);
    string x;
    int len = 0;
    mid >> x;
    a.push(x);
    while (len++ == s.size()) {
        mid >> x;
        if (a.top() != x || a.empty())
            a.push(x);
        else
            a.pop();
    }
    return a.empty();
}

int main() {
    string s, ss;
    cin >> s >> ss;
    if (check(s) && check(ss))
        cout << "legal" << endl;
    else
        cout << "inlegal" << endl;
    return 0;
}

6-2:

#include<stdio.h>
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
using namespace std;

int n, m;
const int max = 100;
int main() {
    cin >> n >> m;
    int i = 0, j;
    vector<int>s, ss;
    while (i++ < n) {
        cin >> j;
        s.push_back(j);
    }
    i = 0, j = 0;
    string s1, mid;
    while (i++ < m) {
        cin >> s1;
        int k = stoi(s1,0,2);
        ss.push_back(s[k]);
    }
    for (auto ch : ss)
        cout << ch;
    return 0;
}

6-3:

#include<string>
#include<cstdio>
#include<iostream>
#include<sstream>
#include<vector>

using namespace std;
vector<string>str[2];
int n;

struct node {
    string val;
    node *left, *right;
    node():left(NULL),right(NULL){}
};

node* newnode() {
    return new node();
}

node *build(string s, string ss, int len1) {
    if (!len1)
        return NULL;
    int len = ss.find(s.front());
    node *root = newnode();
    root->val = s.front();
    root->left = build(s.substr(1), ss, len);
    root->right = build(s.substr(len + 1), ss.substr(len + 1), len1 - len - 1);
    return root;
}

void print(node *root) {
    if (root->left != NULL)
        print(root->left);
    if (root->right != NULL)
        print(root->right);
    str[n].push_back(root->val);
}

int main() {
    cin >> n;
    int j = n - 1;
    while (n--) {
        string s, ss;
        cin >> s >> ss;
        node *root = build(s, ss, s.size());
        print(root);
    }
    for (; j >= 0; j--) {
        for (auto i : str[j])
            cout << i;
        cout << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/romaLzhih/p/9489855.html