pat-top 1004. To Buy or Not to Buy
来源:互联网 发布:端口号占用查询 编辑:程序博客网 时间:2024/05/21 21:41
https://www.patest.cn/contests/pat-t-practise/1004
dfs+剪枝,但是依然有2个case TLE,最后像http://blog.csdn.net/jtjy568805874/article/details/50759483 使用了寻找近似解的方法。
#include <cstdio>#include <iostream>#include <string>#include <climits>#include <cstring>#define _CRT_NONSTDC_NO_WARNINGSusing namespace std;const int limitn = 200;string items[102];int colors[256];bool EvaColors[256];int nColors,n, need;int minCost = INT_MAX,cnt=0;void dfs(int l,int cost) {cnt++; if (cnt > limitn) return;if (minCost < cost) return ;if (l == n) return;int len = items[l].length();int currentColors[256];for (int i = 0; i < 256; i++){currentColors[i] = colors[i];}int currentNeed = need;int currentCost = cost;//Buyfor (int i = 0; i < len; i++){if (colors[(int)items[l][i]] !=0) {colors[(int)items[l][i]]--;need--;}else {cost++;}}if (need == 0) {if (minCost > cost) {minCost = cost;}}dfs(l + 1, cost);//not Buyfor (int i = 0; i < 256; i++){colors[i] = currentColors[i];}need = currentNeed;dfs(l + 1, currentCost);}int main(){string a,b;int pre_n;cin >> a >> pre_n;memset(colors, 0, sizeof(colors));memset(EvaColors, false, sizeof(colors));nColors = a.length();need = nColors;for (int i = 0; i < nColors; i++){colors[(int)a[i]]++;EvaColors[(int)a[i]] = true;}for (int i = 0; i < pre_n; i++){cin >> b;int len = b.length();bool isUseful = false;for (int j = 0; j < len; j++){if (EvaColors[(int)b[j]]) isUseful = true;if (colors[(int)b[j]] != 0) {colors[(int)b[j]] --;need--;}}if (isUseful) items[n++] = b;}if (need != 0) {printf("No %d\n", need);return 0;}for (int i = 0; i < nColors; i++){colors[(int)a[i]]++;}need = nColors;dfs(0,0);printf("Yes %d\n", minCost); return 0;}
0 0
- pat-top 1004. To Buy or Not to Buy
- PAT 1004. To Buy or Not to Buy
- 1004. To Buy or Not to Buy
- 1004. To Buy or Not to Buy
- PAT (Top Level) Practise 1004To Buy or Not to Buy - Hard Version (35)
- PAT A 1092. To Buy or Not to Buy (20)
- PAT 1092. To Buy or Not to Buy
- PAT 1092. To Buy or Not to Buy (20)
- pat 1092. To Buy or Not to Buy (20)
- PAT 1092. To Buy or Not to Buy(STL)
- PAT 1092. To Buy or Not to Buy (20)
- 【PAT】1092. To Buy or Not to Buy (20)
- PAT 1092. To Buy or Not to Buy (20)
- PAT-A 1092. To Buy or Not to Buy
- PAT--1092. To Buy or Not to Buy
- 【PAT】1092. To Buy or Not to Buy
- PAT A1092. To Buy or Not to Buy (20)
- PAT-A 1092. To Buy or Not to Buy (20)
- 在Jupyter notebook中配置和使用spark
- iOS 21种设计模式之单例(Singleton)
- 深度神经网络(DNN)模型与前向传播算法
- 临时记录
- 1103. Integer Factorization
- pat-top 1004. To Buy or Not to Buy
- jenkins+gitlab 安装和配置
- Java中Map类的使用
- PHP遍历删除目录和目录下所有文件
- iOS 21种设计模式之抽象工厂(Abstract Factory)
- android bind aidl 实例理解
- MLlib中K-means流程
- 纯CSS实现的好看的边框
- linux系统在文件夹下查找含有指定字符串的文件的方法