博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #267 (Div. 2)D(DFS+单词hash+简单DP)
阅读量:4963 次
发布时间:2019-06-12

本文共 2683 字,大约阅读时间需要 8 分钟。

D. Fedor and Essay
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

After you had helped Fedor to find friends in the «Call of Soldiers 3» game, he stopped studying completely. Today, the English teacher told him to prepare an essay. Fedor didn't want to prepare the essay, so he asked Alex for help. Alex came to help and wrote the essay for Fedor. But Fedor didn't like the essay at all. Now Fedor is going to change the essay using the synonym dictionary of the English language.

Fedor does not want to change the meaning of the essay. So the only change he would do: change a word from essay to one of its synonyms, basing on a replacement rule from the dictionary. Fedor may perform this operation any number of times.

As a result, Fedor wants to get an essay which contains as little letters «R» (the case doesn't matter) as possible. If there are multiple essays with minimum number of «R»s he wants to get the one with minimum length (length of essay is the sum of the lengths of all the words in it). Help Fedor get the required essay.

Please note that in this problem the case of letters doesn't matter. For example, if the synonym dictionary says that word cat can be replaced with word DOG, then it is allowed to replace the word Cat with the word doG.

Input

The first line contains a single integer m (1 ≤ m ≤ 105) — the number of words in the initial essay. The second line contains words of the essay. The words are separated by a single space. It is guaranteed that the total length of the words won't exceed 105 characters.

The next line contains a single integer n (0 ≤ n ≤ 105) — the number of pairs of words in synonym dictionary. The i-th of the next n lines contains two space-separated non-empty words xi and yi. They mean that word xi can be replaced with word yi (but not vise versa). It is guaranteed that the total length of all pairs of synonyms doesn't exceed 5·105 characters.

All the words at input can only consist of uppercase and lowercase letters of the English alphabet.

Output

Print two integers — the minimum number of letters «R» in an optimal essay and the minimum length of an optimal essay.

Sample test(s)
input
3AbRb r Zz4xR abRbaA xrzz Zxr y
output
2 6
input
2RuruRu fedya1ruruRU fedor
output
1 10
题意:先给出一些单词,然后再给出一个字典,一对一对给出,代表前面的单词能够被后面的替换
            然后要求的是,将原来给出的单词用字典里的单词替换,使得含的R字符最少,假设有多种情况还要使得总字符数最少
思路:先给每种单词hash一下,能够用map实现
            然后将每种单词的R字符数算出来,记为R[i],将每种单词的长度计算出来,记为L[i]
            然后就是简单DP了,dp[i]表示单词 i 的最优的替换的单词,这里的 i 是单词hash后的值
            这个DP能够用DFS来暴搜,先给全部单词按最优值排序,然后按顺序暴搜就好了,搜过的单词就不要反复搜了,会超时的
            然后注意一下建边,假设单词 i 能被单词 j 替换,则从 j 向 i 连一条边
            最后注意的是结果要用long long

转载于:https://www.cnblogs.com/mfrbuaa/p/4316078.html

你可能感兴趣的文章
iPhone在日本最牛,在中国输得最慘
查看>>
动态方法决议 和 消息转发
查看>>
js 基础拓展
查看>>
C#生成随机数
查看>>
Android应用程序与SurfaceFlinger服务的连接过程分析
查看>>
Java回顾之多线程
查看>>
机电行业如何进行信息化建设
查看>>
9、总线
查看>>
Git 笔记 - section 1
查看>>
2018 Multi-University Training Contest 10 - TeaTree
查看>>
2018 Multi-University Training Contest 10 - Count
查看>>
HDU6203 ping ping ping
查看>>
《人人都是产品经理》书籍目录
查看>>
如何在git bash中运行mysql
查看>>
OO第三阶段总结
查看>>
构建之法阅读笔记02
查看>>
DataTable和 DataRow的 区别与联系
查看>>
检索COM 类工厂中CLSID 为 {00024500-0000-0000-C000-000000000046}的组件时失败
查看>>
mysql数据库中数据类型
查看>>
Fireworks基本使用
查看>>