Codeforces Round #267 (Div. 2)D(DFS+单词hash+简单DP)
2 seconds
256 megabytes
standard input
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.
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.
Print two integers — the minimum number of letters «R» in an optimal essay and the minimum length of an optimal essay.
3
AbRb r Zz
4
xR abRb
aA xr
zz Z
xr y
2 6
2
RuruRu fedya
1
ruruRU fedor
1 10
Codeforces Round #267 (Div. 2)D(DFS+单词hash+简单DP)的更多相关文章
- Codeforces Round #267 (Div. 2) C. George and Job(DP)补题
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...
- Codeforces Round #267 (Div. 2) C. George and Job (dp)
wa哭了,,t哭了,,还是看了题解... 8170436 2014-10-11 06:41:51 njczy2010 C - George and Jo ...
- Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)
题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度, ...
- Codeforces Round #574 (Div. 2)——C. Basketball Exercise(简单DP)
题目传送门 题意: 输入n,给出两组均为 n个数字的数组a和b,轮流从a和b数组中取出一个数字,要求严格按照当前所选数字的数组下标比上一个所选数字的数组下标更大,计算能够取出的数字加起来的总和最大能为 ...
- 01背包 Codeforces Round #267 (Div. 2) C. George and Job
题目传送门 /* 题意:选择k个m长的区间,使得总和最大 01背包:dp[i][j] 表示在i的位置选或不选[i-m+1, i]这个区间,当它是第j个区间. 01背包思想,状态转移方程:dp[i][j ...
- Codeforces Round #267 (Div. 2)
A #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> ...
- Codeforces Round #130 (Div. 2) C - Police Station 最短路+dp
题目链接: http://codeforces.com/problemset/problem/208/C C. Police Station time limit per test:2 seconds ...
- Codeforces Round #196 (Div. 2) D. Book of Evil 树形dp
题目链接: http://codeforces.com/problemset/problem/337/D D. Book of Evil time limit per test2 secondsmem ...
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...
随机推荐
- grep之字符串搜索算法Boyer-Moore由浅入深(比KMP快3-5倍)(转)
这篇长文历时近两天终于完成了,前两天帮网站翻译一篇文章“为什么GNU grep如此之快?”,里面提及到grep速度快的一个重要原因是使用了Boyer-Moore算法作为字符串搜索算法,兴趣之下就想了解 ...
- Android 建立View 圆角
虽然很easy,不过还是录制. 混合参观 在drawable文件下 创建一个布局文件corners_bg.xml <?xml version="1.0" encoding=& ...
- 设计模式--简单工厂VS工厂VS抽象工厂
前几天我一直在准备大学毕业生,始终绑起来,如今,终于有时间去学习设计模式.我们研究今天的话题是植物三口之家的设计模式的控制--简单工厂VS工厂VS抽象工厂. 经过细心推敲,我们不难得出:工厂模式是简单 ...
- jquery.validate.unobtrusive
ASP.NET MVC Unobtrusive JavaScript 实现 onfocusout 验证, onfocusin 清除错误 在 ASP.NET MVC 中启用 Unobtrusive Ja ...
- MyBatis与Spring设置callSettersOnNulls
项目中集成Mybatis与Spring,使用的是Mybatis3.2.7,以及Spring4.0.5,mybatis-spring-1.2.2;由于项目组成员想要偷懒,将数据从DB中查询出来时须要将字 ...
- SVN目录对号图标(更新、冲突)不显示
长谈想知道,大约SVN这些冲突.变化.加入.不显示问题etc目录下的复选图标,退房的在线信息,多数说的更改icon的settings,后来,一点点仔细阅读SVN配有英文说明文档,我相信,改变是有点问题 ...
- BZOJ 2115 Wc2011 Xor DFS+高斯消元
标题效果:鉴于无向图.右侧的每个边缘,求一个1至n路径,右上路径值XOR和最大 首先,一个XOR并能为一个路径1至n简单的路径和一些简单的XOR和环 我们开始DFS获得随机的1至n简单的路径和绘图环所 ...
- win9x_win2k下对物理磁盘的操作
void CReadSectorDlg::OnReadButton() { UpdateData (TRUE) ; CFile m_Sector_file ; char * buffer ; if ( ...
- angularjs从零开始(一)
简介 AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了.所以我做了一些工作(你也可以觉得是小 ...
- 最新Oracle 和 mysql 的对比参照----开发篇(转)
Oracle mysql 对比版本 Release 10.2.0.1.0 XE windowsXP 5.0.45-community-nt-log MySQL Community Edition ( ...