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 ...
随机推荐
- iOS 组件化开发之使用CocoaPod制作自己的远程私有库
随着应用需求逐步迭代,应用的代码体积将会越来越大,为了更好的管理应用工程,我们开始借助CocoaPods版本管理工具对原有应用工程进行拆分.但是仅仅完成代码拆分还不足以解决业务之间的代码耦合,为了更好 ...
- STA之RC网
STA的主要工作是计算电路网络的延时,如今的电路网络还是由CMOS cell和net组成的,所以STA所要计算的延时仍是电容的充放电时间.等量子计算机普及的时候,如今的这一套理论都将随着科技的进步被丢 ...
- 【网搜】禁止 number 输入非数字(Android仍有问题)
目的:使用 number 表单,让其只可输入数字. 问题:ios 可正常限制,Android 仍可输入 [ e | . | - | + ] 这4个字符.猜测这4个字符在数值中为科学记数.小数 ...
- svn怎么修改用户名和密码
链接:https://blog.csdn.net/qq_36826506/article/details/80915431
- C++中map的介绍用法以及Gym题目:Two Sequences
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字(key),每个关键字只能在map中出现一次,第二个可能称为该关键字的值(value))的数据 处理能力,由于这个特性,它完成有可能 ...
- json 字符串 <----> json 对象
一,字符串 -->JSON对象 1,转换函数 JSON.parse(json_str): 2,$.parseJSON(json_str): 用的是 jquery 的插件 所以需要引入 jq. ...
- dp(装箱)
请小伙伴们对自己AC的题目进行标记,注意每人只能标记一次!不知道的不要标记,恶意标记者将回收账号!!! 问题 B: 装箱问题 时间限制: 1 Sec 内存限制: 128 MB[提交] [状态] 题目 ...
- centos7一步一步搭建docker tomcat 及重点讲解
系统环境:centos7.7 (VMware中) image版本:tomcat:8-jdk8-openjdk (截止2020.01.10该系列版本) 安装步骤参考文章:https://www.jian ...
- 07-Docker-Image深入理解
目录 07-Docker-Image深入理解 参考 镜像简介 什么是Docker镜像 什么是Docker容器 镜像结构 镜像特性 镜像层 容器层 镜像存储 07-Docker-Image深入理解 Do ...
- Django框架中的Cookie和Session
学习内容: (1)cookie (2)session Web是基于请求/响应模式,HTTP协议是无状态的,但是基于 Internet的各种服务系统应运而生,建立商业站点或者功能比较完善的个人站点,常常 ...