暴力搜索加剪枝~

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
string t;
string s[maxn];
int pos[maxn],pos1[maxn];
int N;
int nowLength;
int minLength=1e9;
int tle;
void dfs (int v,int cnt) {
if (tle==) return;
tle++;
if (nowLength>minLength) return;
if (cnt>=t.length()) {
minLength=min(minLength,nowLength);
return;
}
v++;
while (v<=N) {
int a=cnt;
int pos2[maxn];
for (int i=;i<s[v].length();i++) pos2[s[v][i]]=pos1[s[v][i]];
for (int i=;i<s[v].length();i++)
if (pos[s[v][i]]>pos1[s[v][i]]) cnt++,pos1[s[v][i]]++;
nowLength+=s[v].length();
dfs (v,cnt);
cnt=a;
for (int i=;i<s[v].length();i++) pos1[s[v][i]]=pos2[s[v][i]];
nowLength-=s[v].length();
v++;
}
}
int main () {
cin>>t>>N;
for (int i=;i<=N;i++) cin>>s[i];
for (int i=;i<t.length();i++) pos[t[i]]++;
dfs (,);
int ans=;
for (int i=;i<=N;i++)
for (int j=;j<s[i].length();j++)
if (pos[s[i][j]]) pos[s[i][j]]--;
for (int i=;i<t.length();i++) ans+=pos[t[i]],pos[t[i]]=;
if (minLength<1e9) printf ("Yes %d",minLength-t.length());
else printf ("No %d",ans);
return ;
}

PAT T1004 To Buy or Not to Buy - Hard Version的更多相关文章

  1. PAT 1092 To Buy or Not to Buy

    1092 To Buy or Not to Buy (20 分)   Eva would like to make a string of beads with her favorite colors ...

  2. pat 1092 To Buy or Not to Buy(20 分)

    1092 To Buy or Not to Buy(20 分) Eva would like to make a string of beads with her favorite colors so ...

  3. PAT_A1092#To Buy or Not to Buy

    Source: PAT A1092 To Buy or Not to Buy (20 分) Description: Eva would like to make a string of beads ...

  4. PAT1092:To Buy or Not to Buy

    1092. To Buy or Not to Buy (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  5. 1092 To Buy or Not to Buy (20 分)

    1092 To Buy or Not to Buy (20 分) Eva would like to make a string of beads with her favorite colors s ...

  6. poj1092. To Buy or Not to Buy (20)

    1092. To Buy or Not to Buy (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  7. PAT (Advanced Level) Practise - 1092. To Buy or Not to Buy (20)

    http://www.patest.cn/contests/pat-a-practise/1092 Eva would like to make a string of beads with her ...

  8. PAT甲级——A1092 To Buy or Not to Buy【20】

    Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy ...

  9. PAT Advanced 1092 To Buy or Not to Buy (20) [Hash散列]

    题目 Eva would like to make a string of beads with her favorite colors so she went to a small shop to ...

随机推荐

  1. Java - JVM - jinfo

    1. 概述 jinfo java 运行时的配置工具 查看 设置 2. 环境 idea 2018.2 java jdk8 3. 准备 写个 死循环 概述 需要查看 jvm 的信息, 必须有个运行中的 j ...

  2. 每天进步一点点------Sobel算子(1)

    void MySobel(IplImage* gray, IplImage* gradient) { /* Sobel template a00 a01 a02 a10 a11 a12 a20 a21 ...

  3. JavaScript 推箱子游戏

    推箱子游戏的 逻辑非常简单,但是如果不动手的话,还是不太清楚.我在这里讲一下自己的思路. 制作推箱子,首先要有自己的设计素材.如下我也是网上找的素材 第二步,理清游戏的规则. 游戏规则: 1.小人将箱 ...

  4. +(new Date())

    +(new Date()) 等于 new Date().getTime();展示 1561003191879 毫秒时间戳

  5. Windows Server 2016安装.NET Framework 3.5

    1.打开“服务器管理器” 2.点击“添加角色和功能” 3.点击“下一步” 4.点击“下一步” 5.点击“下一步” 6.点击“下一步” 7.勾选“.NET Framework 3.5功能”,点击“下一步 ...

  6. 6_10 下落的树叶(UVa699)<二叉树的DFS>

    每年到了秋天树叶渐渐染上鲜艳的颜色,接着就会落到树下来.假如落叶发生在二叉树,那会形成多大的树叶堆呢?我们假设二叉树中的每个节点所落下的叶子的数目等于该节点所储存的值.我们也假设叶子都是垂直落到地面上 ...

  7. centOs安装出现No package git available的解决办法

    来源地址 [http://chinacheng.iteye.com/blog/1825538 ] centos安装git 下载源代码安装后,git clone出现“fatal unable to fi ...

  8. vue的页面怎么显示到android的webview中

    链接:https://www.jianshu.com/p/0dd98476bba0

  9. ST股

    一.简介: 意即“特别处理”.该政策针对的对象是出现财务状况或其他状况异常的. 1998年4月22日,沪深交易所宣布,将对财务状况或其它状况出现异常的上市公司股票交易进行特别处理(Special tr ...

  10. 微信-获取openid

    第一步 首先把微信的支付流程与相关的文档熟悉一遍,具体的支付逻辑是怎么实现的,心里要有一定的路数,开发的时候一边看文档,一边写,再一边调试这是最好的选择,首先阅读微信开发文档,因为我们这次是做公众号支 ...