PAT T1004 To Buy or Not to Buy - Hard Version
暴力搜索加剪枝~
#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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- PAT1092:To Buy or Not to Buy
1092. To Buy or Not to Buy (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- vs查看动态库依赖
dumpbin是VS自带的工具,进入Visual Studio 的命令提示 查看程序或动态库所依赖的动态库 dumpbin /dependents abc.exe 查看动态库的输出函数 dumpbi ...
- Eclipse使用段注释格式化代码后混乱情况解决
今天在Eclipse写代码用到段注释代码块注释一个方法,习惯使用格式化代码,结果一看格式化后的代码就乱了.就像下面那样. 觉得太乱了,而且不好对比检查.如果取消注释中间的**还保存了的,好纠结.于是就 ...
- centos 添加rpmfusion源,yum 安装 ffmpeg
rpmfusion提供了,在Fedora和 centos 源之外的其他yum 源 例如ffmpeg 在 centos 基础源和epel-release 里面都没有, 但是可以在rpmfusion中找到 ...
- JDBC--处理Blob
1.LOB(Large Objects)大对象,是用来存储大量的二进制和文本数据的一种数据类型(一个LOB字段可存储多达4GB的数据) --LOB分类两种类型:1)内部LOB: 2)外部LOB: -- ...
- 深入delphi编程理解之消息(一)WINDOWS原生窗口编写及消息处理过程
通过以sdk方式编制windows窗口程序,对理解windows消息驱动机制和delphi消息编程有很大的帮助. sdk编制windows窗口程序的步骤: 1.对TWndClass对象进行赋值; 2. ...
- 请描述一下 cookies,sessionStorage 和 localStorage 的区别
原文:http://blog.csdn.net/lxcao/article/details/52809939 相同点:都存储在客户端不同点: 1.存储大小 cookie数据大小不能超过4k. sess ...
- Vue - 让水平滚动条(scroll bar)固定在浏览器的底部
效果 踩坑经历 TLDR; 在几个小时的google和stack overflow的苦苦搜索后,无果. 经过自我思考,想到了一种实现方法: 整个页面是一个盒子,要出现滚动条,必然里面的元素要溢出.也即 ...
- 普及C组第一题(8.1)
1999. [2015.8.6普及组模拟赛]Wexley接苹果(apple) 题目: Wexley最近发现了一个古老的屏幕游戏.游戏的屏幕被划分成n列.在屏幕的底端,有一个宽为m列的篮子(m<n ...
- scala安装此时不应有 \scala\bin\scala.bat
scala安装目录有空格导致的,不应该有空格
- Java 判断五子棋五子相连
#开始 最近在忙着做一个基于酷Q的QQ机器人,想到了做游戏,第一个想到的霸气点的游戏就是五子棋啊 ` _>` 因为没有图形界面的原因 所有核心就是判断是否在棋盘上出现了五个棋子连在一起的情况 ...