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 ...
随机推荐
- 题解【洛谷P2853】[USACO06DEC]牛的野餐Cow Picnic
题目描述 The cows are having a picnic! Each of Farmer John's \(K (1 ≤ K ≤ 100)\) cows is grazing in one ...
- MySQL表的操作02
[1]设置非空约束(NOT NULL ,NK)--->>>目的是f防止某些字段中的内容为空 CREATE TABLE +table_name ( 属性名 数据类型 NOT N ...
- PHP 超全局变量之$_FILES
$_FILES——通过 HTTP POST 方式上传到当前脚本的项目的数组. 假设我们上传文件字段name='userfile',$_FILES数组里包括: $_FILES['userfile'][' ...
- 一些封装的php函数
swoole群中奥总共享的创建文件夹: function make_dir($folder){ $reval = false; if (!file_exists($folder)){ /* 如果目录不 ...
- opencv:通道的分离与合并
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...
- Python--比较两个字典部分value是否相等(可以用于接口自动化)
eg:例如你调用了一个新增的接口,以往功能测试的话,你再web端新增一个店铺之后,你肯定要去数据库中查看,这些数据插入的对不对,是否正确的插入了每个字段 # 比较两个字典部分是否相等 def comp ...
- Markdown Learning Notes
Markdown 教程 Markdown 是一种轻量级标记语言,它允许人们使用易读易写的纯文本格式编写文档. Markdown 语言在 2004 由约翰·格鲁伯(英语:John Gruber)创建. ...
- 封装ajax库,post请求
http状态码406是服务器无法根据客户端请求的内容特性完成请求 //整站功能方法库封装ajax请求,这里只针对post var methods = { //全站ajax请求状态处理 ajax: fu ...
- Go语言内置包之strconv
文章引用自 Go语言内置包之strconv Go语言中strconv包实现了基本数据类型和其字符串表示的相互转换. strconv包 strconv包实现了基本数据类型与其字符串表示的转换,主要有以下 ...
- angular常用命令整理
1.创建项目 ng new 命令 描述ng new <project-name> [options] 创建一个新的 Angular 项目,默认在当前所在目录下参数 描述--dry-run ...