Accepted丶 Personal Training (数据结构 && STL) 【未完待续】

Problem A ZOJ 1004 Anagrams by Stack
题意:给你两个串,一个原串,一个目标串,让你借用栈来得出所有的使原串变成目标串的序列。
解法:借用DFS递归求解,先进栈,后出栈,当出入栈次数刚好为原单词的长度时,目标单词构造完毕,输出操作序列

#include <bits/stdc++.h>

#define INF 0x3f3f3f3f
#define eps 1e-8
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 1e9 + 7;
using namespace std;

string a,b;
stack <char> build;
vector <char> operate;

int len;

void dfs(int ipush,int ipop)
{
    if(ipush == len && ipop == len)
    {
        for(int i = 0;i < operate.size();i++)
            cout << operate[i] << " ";
        cout << endl;
    }
    if(ipush < len)
    {
        build.push(a[ipush]);
        operate.push_back('i');
        dfs(ipush + 1,ipop);
        build.pop();
        operate.pop_back();
    }
    if(ipop < len && ipop < ipush && build.top() == b[ipop])
    {
        char tc = b[ipop];
        build.pop();
        operate.push_back('o');
        dfs(ipush,ipop + 1);
        build.push(tc);
        operate.pop_back();
    }
}


int main()
{
    //freopen("int.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(cin >> a >> b){
        len = a.length();
        cout << "[" << endl;
        dfs(0,0);
        cout << "]" << endl;
    }
    return 0;
}

Problem B ZOJ 1094 Matrix Chain Multiplication
题意:给定你n个矩阵的名称和行数、列数,再给定你指定的矩阵相乘的表达值,让你用栈算出这个表达式中矩阵乘法的次数,不合法输出

"error" 

分析:计算过程分为以下三种情况:
1)左括号:不影响结果,直接跳过
2)右括号:弹出栈顶的两个矩阵,计算他们的乘积,如果不能相乘,输出error,否则将新得到的矩阵压人栈中。
3)矩阵:压入栈中

#include <bits/stdc++.h>

#define INF 0x3f3f3f3f
#define eps 1e-8
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 1e9 + 7;
using namespace std;

struct Node
{
    int row,col;
};

int main()
{
    //freopen("int.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n;
    char name;
    cin >> n;
    map <char,Node> m;
    for(int i = 0;i < n;i++){
        cin >> name;
        cin >> m[name].row;
        cin >> m[name].col;
    }
    string exp;
    while(cin >> exp)
    {
        int i;
        int ans = 0;
        stack <Node> Array;
        for(i = 0;i < exp.size();i++)
        {
            if(exp[i] == '(')
                continue;
            if(exp[i] == ')')
            {
                Node b = Array.top();
                Array.pop();
                Node a = Array.top();
                Array.pop();
                if(a.col != b.row)
                {
                    cout << "error" << endl;
                    break;
                }
                ans += a.row * b.row * b.col;
                Node tmp = {a.row,b.col};
                Array.push(tmp);
            }
            else
                Array.push(m[exp[i]]);
        }
        if(i == exp.size())
            cout << ans << endl;
    }
    return 0;
}

Problem C ZOJ 1011 NTA
Problem D ZOJ 1062 Trees Made to Order
Problem E ZOJ 1097 Code the Tree
Problem F ZOJ 1156 Unscrambling Images
Problem G ZOJ 1167 Trees on the Level
Problem H ZOJ 1016 Parencodings
Problem I ZOJ 1944 Tree Recovery
Problem J ZOJ 2104 Let the Balloon Rise

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值