《暑假每日一题》Week 4:6.27 - 7.3

Day 1 :AcWing 693. 行程排序

题目链接:

AcWing 693. 行程排序

关键词:

哈希表

题意:

输入每 2 行一组,表示一张机票的信息,每行包含一个字符串,其中第一行表示出发地,第二行表示目的地,求她的行程是什么

思路:

用一个哈希表存点,再用另一个哈希表存边,先求出入度为 0 的点,再遍历每个边输出行程

AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>

using namespace std;


int main( )
{

    int T;
    cin >> T;
    int n;
    for (int op = 1; op <= T; op ++)
    {
        // 存数据
        cin >> n;
        string start, end;
        unordered_map<string,string> next;
        unordered_set<string> s;
        while (n --)
        {
            cin >> start >> end;
            next[start] = end;
            s.insert(end);
        }
        
        string hh;
        for (auto item : next)
            if (!s.count(item.first))
            {
                hh = item.first;
                break;    
            }
            
        printf("Case #%d: ", op);
        while (next[hh].size())
        {
            cout << hh << '-' << next[hh] << ' ';
            hh = next[hh];
            
        }
        cout << endl;
    }
    return 0;
}




Day 2:AcWing 3428. 放苹果

题目链接:

AcWing 3428. 放苹果

关键词:

dfs

题意:

M个苹果放到N个盘子,盘子可以不放,求所有方案数,盘子相对顺序不同,例如 5,1,1 和 1,5,1 算作同一种分法。

思路:

递归搜索每个盘子上的苹果

AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

int m, n;

int dfs(int u, int sum, int last)
{
    if (u == n)
    {
        if (sum == 0)   return 1;
        return 0;
    }
    
    int res = 0;
    for (int i = last; i <= sum; i ++)
        res += dfs(u + 1, sum - i, i);
    
    return res;
}

int main( )
{
    while (cin >> m >> n)
    {
        cout << dfs(0, m, 0) << endl;
    }
    return 0;
}




Day 3 :AcWing 3477. 简单排序

题目链接:

AcWing 3477. 简单排序

关键词:

哈希表、vector

题意:

给定一个包含 n 个整数的数组,请你删除数组中的重复元素并将数组从小到大排序后输出。

思路:

哈希表去重,vector排序

AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <unordered_set>
#include <vector>

using namespace std;

int main( )
{
    int n;
    cin >> n;
    unordered_set<int> hash;
    while(n --)
    {
        int a;
        cin >> a;
        hash.insert(a);
    }
    vector<int> s;
    for (auto item : hash)
        s.push_back(item);

    sort(s.begin(), s.end());    

    for (auto item : s)
        cout << item << ' ';
    cout << endl;
    return 0;
}





Day 4 :AcWing 3531. 哈夫曼树

题目链接:

AcWing 3531. 哈夫曼树

关键词:

小根堆、哈夫曼树

题意:

给定 n 个数求最短带权路径

思路:

构造哈夫曼树

AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>

using namespace std;

int main()
{
    int n;
    cin >> n;
    
    // 小根堆
    priority_queue<int, vector<int>, greater<int>> heap;
    
    // 存点
    while (n --)
    {
        int x;
        cin >> x;
        heap.push(x);
    }
    
    // 建哈夫曼树
    int ans = 0;    // 带权路径长度
    while (heap.size() > 1)     // 当堆中有元素
    {
        int x = heap.top(); // 取堆顶
        heap.pop();         // 弹出
        
        int y = heap.top(); // 取堆顶
        heap.pop();         // 弹出
        
        int sum = x + y;
        ans += sum;
        
        heap.push(sum);
    }
    
    cout << ans << endl;
    return 0;
}




Day 5 :AcWing 3438. 数制转换

题目链接:

AcWing 3438. 数制转换

关键词:

进制转换、秦九韶算法、短除法

题意:

将 a 进制下的数 n 转为 b进制

思路:

先用秦九韶算法转十进制,再用短除法转b进制

AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

int a, b;
string s;

int main( )
{
    cin >> a >> s >> b;
    

    return 0;
}




Day 6 :AcWing 3511. 倒水问题

题目链接:

AcWing 3511. 倒水问题

关键词:

dfs

题意:

给定A、B、C三个杯子的容量上限,并且只有C是满的,其他两个都是空的,将一个杯子 x 中的水倒到另一个杯子 y 中,当 x 空了或者 y 满了时就停止(满足其中一个条件才停下)。在操作全部结束后,C 中的水量有多少种可能性。

思路:

dfs进行搜索一共只有六种倒水情况: A -> B、A -> C、B -> C 、B -> A 、B -> C 、C -> A 、C ->B

AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_set>

using namespace std;

typedef long long LL;

const LL B = 10000;

int C[3];
unordered_set<LL> states;
unordered_set<int> cs;

LL get(int c[])
{
    return c[2] * B * B + c[1] * B + c[0];
}

void pour(int c[], int i, int j)
{
    int t = min(c[i], C[j] - c[j]);
    c[i] -= t, c[j] += t;
}

void dfs(int c[])
{
    states.insert(get(c));
    cs.insert(c[2]);

    int a[3];
    
    // 模拟倒水
    for (int i = 0; i < 3; i ++ )
        for (int j = 0; j < 3; j ++ )
            if (i != j)
            {
                memcpy(a, c, sizeof a);
                pour(a, i, j);
                if (!states.count(get(a)))
                    dfs(a);
            }
}

int main()
{
    while (cin >> C[0] >> C[1] >> C[2])     // C 数组表示每个杯子的容量上限, c 数组表示当前杯子的状态
    {
        states.clear();
        cs.clear();
        
        int c[3] = {0, 0, C[2]};
        dfs(c);
        
        cout << cs.size() << endl;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值