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 ...
随机推荐
- Git - Windows 下, gitbash 打开资源管理器
1. 概述 windows 下 gitbash 打开 资源管理器 2. 场景 资源管理唤起 gitbash 步骤 进入目录 鼠标右击 在 弹出菜单 中, 找到 Git Bash Here 结果 打开一 ...
- 【游戏体验】Buttonhunt(鼠标寻找按钮)
虽然画风千奇百变 但这款益智游戏的趣味性相当高 推荐试玩 个人测评 游戏性 9/10 音乐 4/10 剧情 6/10 总评 19/30
- JDBC 基础用法学习
JDBC概述 java 数据库链接,sun公司退出的 java 访问数据库的标准规范接口 是一种用于执行SQL语句的 java API 可以作为多种关系数据库提供统一接口 是一组 java 工具类和接 ...
- 输入两个正整数num1、num2,计算并输出它们的和、差、积、整数商和余数
课本例题 /*输入两个正整数num1.num2,计算并输出它们的和.差.积.整数商和余数.*/ #include<stdio.h> int main() { int num1, num2; ...
- Layui自定义模块的使用方式
为什么要自定义模块呢?好处很多.比如可以大量重用代码...... 根据layui官方的文档说明.首先第一步是要确定你要扩展的模块名称 现在做的是登录功能.因此扩展模块名叫 login 使用layui ...
- acm数论之旅(转载) -- 逆元
ACM数论之旅6---数论倒数,又称逆元(我整个人都倒了( ̄﹏ ̄)) 数论倒数,又称逆元(因为我说习惯逆元了,下面我都说逆元) 数论中的倒数是有特别的意义滴 你以为a的倒数在数论中还是1/a吗 ( ...
- canvas的描述
// 1.找到DOM节点 const canvas = document.getElementById('canvas'); // 2.画笔 --- canvas的上下文对象 const ctx = ...
- 分布式一致性协议 --- Paxos
问题 Paxos 到底解决什么样的问题,动机是什么 Paxos 流程是怎么样的? Paxos 算法的缺陷是什么 概述 Paxos 是分布式一致性算法,根据少数服从多数的原则多个节点确定某个数值.通过学 ...
- RTT学习之sensor设备
Sensor设备的常用操作: 首先查找传感器设置获取设备句柄.rt_device_find 以轮询.FIFO.中断.任意一种方式打开传感器,中断和FIFO需要设置接收回调函数(释放一个信号量给接收线程 ...
- Go_排序
package main import ( "fmt" "sort" "math/rand" ) //1.声明Hero结构体 type He ...