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_WARNINGS
using 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;
//Buy
for (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 Buy
for (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;
}