Dictionary Strings

题目连接:

https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/dictionary-strings

Description

Gopal is preparing for a competitive exam and he has to prepare many topics for it. To remember the concepts better he identified a set of words from each topic. He prepared dictionaries for each of these topics with the set of identified words so that he can refer to them easily.

While recollecting the topics Gopal sometimes could not remember to which dictionary a certain word belongs. After all the hard work, Gopal didn’t want to lose marks due to this confusion. So he requested his friend, Govind, to help him identify a way to check if a word belongs to a dictionary.

Govind, being a very good friend of Gopal, wants to help him do better in the exam. So, after some thought, he finally came up with a solution.

For each dictionary, a string is chosen from which all the words can be made by selecting a subset of the characters from the string and rearranging them. (It is not necessary that the characters are consecutive and/or in the same order as in the string). They called this string a Dictionary String. When confused about to which dictionary a word belongs, Gopal can check if the word can be extracted from the Dictionary String for that dictionary.

To qualify as a Dictionary String, all the letters needed to explicitly form each word of the dictionary must be present in the string. You cannot reuse letters. Thus, the string aab is not a Dictionary String for a dictionary containing the word aaa since this word needs 3 a's whereas the candidate Dictionary String has only two a's.

To help Gopal memorize the Dictionary Strings better, Govind inserted extra characters in some of the Dictionary Strings that appeared harder to memorize. To distinguish those strings from others he calls a string without any extra characters, a Perfect Dictionary String.

Govind would like your help in verifying his program. For a set of words in a dictionary, you should indicate whether a string is a perfect dictionary string and/or a dictionary string. If a word is not a dictionary string, he would like you to tell him the minimum number of characters needed to convert the string to a dictionary string.

Notes:

Some of the test cases are very large, and may require you to speed up input handling in some languages.

In C++, for example, you can include the following line as the first line in your main function to speed up the reading from input:

std::ios_base::sync_with_stdio (false);

And in Java, you can use a BufferedReader to greatly speed up reading from input, e.g.:

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

// Read next line of input which contains an integer:

int T = Integer.valueOf(reader.readLine());

Input

Input begins with a single integer T, 1 <= T <= 100, which denotes number of test cases.

Each test case begins with a line, which contains 2 space-separated integers D and S. D represents the number of words in a dictionary, and S represents the number of potential dictionary strings to be checked. Note that 1 <= D, S <= 100.

Next follows D lines, each containing a word in the dictionary.

The remaining S lines in the test case each contain a potential dictionary string.

Notes: The words in the dictionary and the potential dictionary strings will consist of only lower-case letters. The lengths of these strings are greater than or equal to one character and less than or equal to 40,000 characters.

Output

For each of the S potential dictionary strings, you should output a line with two values separated by a space in the following format:

A1 A2

Where

A1 is either Yes or No denoting if a string is a Dictionary String or not.

If A1 is No, then A2 is the minimum number of characters needed to make the string a Dictionary String. If A1 is Yes, then A2 is Yes if the string is a Perfect Dictionary String, and No otherwise.

Sample Input

1

5 3

ant

top

open

apple

lean

anteplop

antelope

penleantopan

Sample Output

Yes Yes

No 1

Yes No

Hint

For the sample input, there is only one test case with 5 words in it and 3 strings to be checked.

anteplop: contains all the words from the dictionary and no extra characters. So it is both a Dictionary String and a Perfect Dictionary String. Hence, the output is Yes Yes.

antelope: the words “apple” cannot be made from this string. So it is not a Dictionary String and is missing 1 character (‘p’) to become a dictionary string. Hence the output No 1.

penleantopan: all the words of the dictionary can be made from this string but it also contains extra characters that are not required to build the words of the dictionary. So it is a Dictionary String but not a Perfect Dictionary String.

题意

给你d个字符串,然后给再你s个字典串字符串。

你需要对每一个字典串进行判断:(位置无关,可以重新排序)

是否d个字符串都是这个字典串的子串。

如果是,那么问你这个字典串是否所有字符都被用过。

如果不是,那么最少添加多少个字符,能够使得d个字符串都是字典串的子串。

题解

首先我们预处理d个字符串的每个字符出现的最高次数。

然后对于每一个s字符串,如果某个字符小于最高次数,那么显然是不合法的。

如果等于,那么就是YES

如果大于,那么显然不是每一个字符都被用过。

代码

#include<bits/stdc++.h>
using namespace std; int a[26];
int b[26];
void init()
{
memset(a,0,sizeof(a));
}
void solve(){
int d,p;cin>>d>>p;
for(int i=0;i<d;i++){
string s;cin>>s;
memset(b,0,sizeof(b));
for(int j=0;j<s.size();j++)
b[s[j]-'a']++,
a[s[j]-'a']=max(a[s[j]-'a'],b[s[j]-'a']);
}
for(int i=0;i<p;i++)
{
string s;cin>>s;
memset(b,0,sizeof(b));
int flag=0,ans1=0,ans2=0;
for(int j=0;j<s.size();j++)
b[s[j]-'a']++;
for(int j=0;j<26;j++){
if(b[j]<a[j])
{
flag=-1;
ans1+=a[j]-b[j];
}
if(flag!=-1&&b[j]>a[j])
{
flag=1;
ans2+=b[j]-a[j];
}
}
if(flag==-1)
cout<<"No"<<" "<<ans1<<endl;
else if(flag==0)
cout<<"Yes Yes"<<endl;
else
cout<<"Yes No"<<endl;
}
}
int main()
{
std::ios_base::sync_with_stdio (false);
int t;cin>>t;
while(t--)
{
init();
solve();
}
return 0;
}

IEEEXtreme Practice Community Xtreme9.0 - Dictionary Strings的更多相关文章

  1. IEEEXtreme Practice Community Xtreme9.0 - Digit Fun!

    Xtreme9.0 - Digit Fun! 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/di ...

  2. Xtreme9.0 - Communities 强连通

    Xtreme9.0 - Communities 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/c ...

  3. Xtreme9.0 - Light Gremlins 容斥

    Xtreme9.0 - Light Gremlins 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenge ...

  4. Swift3.0 中 Strings/Characters 闲聊

    前言 本篇文章主要浅析字符串\字符在 Swift 和 Objective-C 之间的区别及其简单用法.如有不妥的地方还望大家及时帮忙纠正. 字符串判空 在 swift 语言中空字符串初始化方式常用的有 ...

  5. Xtreme9.0 - Block Art 线段树

    Block Art 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/block-art Descr ...

  6. Xtreme9.0 - Taco Stand 数学

    Taco Stand 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/taco-stand Des ...

  7. Xtreme9.0 - Pattern 3 KMP

    Pattern 3 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/car-spark Descr ...

  8. Xtreme9.0 - Car Spark 动态规划

    Car Spark 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/car-spark Descr ...

  9. Xtreme9.0 - Mr. Pippo's Pizza 数学

    Mr. Pippo's Pizza 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/mr-pipp ...

随机推荐

  1. 工具类 | window批处理杀死指定端口进程

    window批处理杀死指定端口进程,注意保存时使用ansi格式,运行输入端口即可 @echo off setlocal enabledelayedexpansion set /p port=请输入端口 ...

  2. html5 canvas 径向渐变2

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. javascript私有静态成员

    就私有静态成员而言,指的是成员具有如下属性:1.以同一个构造函数创建的所有对象共享该成员.2.构造函数外部不可访问该成员. //构造函数 var Gadget = (function(){ //静态变 ...

  4. 分布式监控工具Ganglia 介绍 与 集群部署.

    如果你目的很明确就是冲着标题来的,不爱看我唠叨,请直接进入第二个分割线之后的内容. 其实之前就是有做Swift监控平台的打算的,但是因为没什么硬性需求么,也不要紧的,就一直搁置了.最近实验室来了个大二 ...

  5. github for Mac 教程

    Mac系统自带git,所有我们使用Mac搬的github客户端,无需安装git,所以使用github for Mac 超级简单,下载安装就好了. 1github for Mac 下载地址:https: ...

  6. MFC里ON_COMMAND_RANGE消息映射的ID问题

    今天在工作中遇到一个问题,一个动态菜单,每个菜单的菜单项ID是我自己定义的,定义如下: #define IDM_SEARCHRECORD0 222240 #define IDM_SEARCHRECOR ...

  7. python技巧 合并两个字典

    python 3.5+ 版本 In [1]: a={'x':2,'y':4} In [2]: b={'c':1,'d':3} In [3]: c={'c':3,'y':6} In [4]:  w={* ...

  8. 洛谷 P1045 【麦森数】快速幂

    不用快速幂,压位出奇迹! 本人是个蒟蒻,不太熟悉快速幂,这里给大家介绍一种压位大法. 让我们来分析一下题目,第一位是送分的,有一个专门求位数的函数:n*log10(2)+1. 然后题目中p<=3 ...

  9. PHP5.6 和PHP7.0区别

    1. PHP7.0 比PHP5.6性能提升了两倍. 2.PHP7.0全面一致支持64位. 3.PHP7.0之前出现的致命错误,都改成了抛出异常. 4.增加了空结合操作符(??).效果相当于三元运算符. ...

  10. 安装odbc驱动

    1.下载对应的驱动 (32位/64位) http://www.oracle.com/technetwork/database/database-technologies/instant-client/ ...