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 ...
随机推荐
- Nuxt 环境搭建已经编写第一个Nuxt应用
在学习Nuxt 之前 首先我们要有node ,然后因为Nuxt 是一个基于 Vue.js 的轻量级应用框架,所以在开发之前需要安装(后面纯属作者猜想并且猜想就是这个原因...) npm install ...
- java将小写金额转换为大写的工具类
public class Tool { private static final String UNIT = "万千佰拾亿千佰拾万千佰拾元角分"; ...
- 每天进步一点点------CORDIC (一)
三角函数计算,Cordic 算法入门 三角函数的计算是个复杂的主题,有计算机之前,人们通常通过查找三角函数表来计算任意角度的三角函数的值.这种表格在人们刚刚产生三角函数的概念的时候就已经有了,它们通常 ...
- 【Webpack】
目录 关于模块化编程 Webpack的工作方式 三个重要的概念 使用Webpack创建一个项目 正式使用Webpack 使用Webpack进行ES6的模块化编程 "本质上,Webpack是一 ...
- Coursera-吴恩达机器学习课程笔记-Week1
参考资料: 吴恩达教授机器学习课程 机器学习课程中文笔记 Week 1 一. 引言 机器学习模型可分为监督学习Superviese learning(每个数据集给出了正确的值)和无监督学习Unsupe ...
- Redis注册成服务
注册服务 redis-server.exe –-service-install redis.windows.conf 删除服务 redis-server –-service-uninstall 开启服 ...
- 如何在JDBC Connection Configuration配置组件上添加控件
如何在JDBC Connection Configuration配置组件上添加控件 最近项目刚上线,闲来无事又把Jmeter的源码拿出来研究研究,最初的目的是想扒一扒Jmeter里数据库处理的逻辑是怎 ...
- [Qt 踩坑] 设置背景就卡退 报错 异常结束或者crashed 0xFFFFFFFF
#开始 我现在是带着崩溃得心情写博客的 这个估计是我遇到的最混蛋的bug 我试图用Qt写个小程序 然后给窗口添加一个背景 就像这样 先不管我那个路径的问题 只要写了那个代码,就一定出错 删除了一点事情 ...
- APP inventor 学习
中文教程:https://web.17coding.net/ 网页在线开发:http://app.gzjkw.net/#1 ai2.appinventor.mit.edu http://appinve ...
- php 单机redis 常用命令
一.Redis连接与认证 //连接参数:ip.端口.连接超时时间,连接成功返回true,否则返回false $ret = $redis->connect('127.0.0.1', 6379, 3 ...