[CF467D] Fedor and Essay
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
十分气愤!!!
不懂为什么他们机房互模成瘾,实在忍不了。 我太菜了,这么水的题都想不出来...
Noip怕是凉了。
首先我们可以把每个出现的字符串当做一个点,把转化关系当做边。
用一个pair类型存储每个字符串的$r$的个数和长度。
然后贪心地想,我们肯定是要让尽可能多的字符串有尽量少的r和尽量少的长度。
把所有字符串放进队列里bfs,类似spfa,如果能更新的话就再次入队。
最后所有更新完之后的1——n的r的个数和长度一定是最短的。
还要注意字符串会有重复,比较坑。
总之我太菜了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <queue>
#include <map>
#include <vector>
using namespace std;
#define reg register
inline char gc() {
static const int BS = << ;
static unsigned char Buf[BS], *st, *ed;
if (st == ed) ed = Buf + fread(st = Buf, , BS, stdin);
return st == ed ? EOF : *st++;
}
#define gc getchar
inline int read() {
int res=;char ch=getchar();bool fu=;
while(!isdigit(ch)) {if(ch=='-')fu=;ch=getchar();}
while(isdigit(ch))res=(res<<)+(res<<)+(ch^), ch=getchar();
return fu?-res:res;
}
#define ll long long
#define pii pair <ll, ll>
#define mkp make_pair
int n, m;
map <string, int> mp;
int tot;
pii memc[];
vector <int> ve[];
ll ans1, ans2;
int cp[]; inline void down(string &s)
{
int len = s.length();
for (reg int i = ; i < len ; i ++)
if (s[i] >= 'A' and s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
} inline void add(string &s)
{
int len = s.length();
down(s);
ll cnt = ;
for (reg int i = ; i < len ; i ++)
if (s[i] == 'r') cnt++;
if (mp.find(s) == mp.end()) {
mp[s] = ++tot;
memc[tot] = mkp(cnt, len);
}
} int main()
{
n = read();
for (reg int i = ; i <= n ; i ++)
{
string s;
cin >> s;
add(s);
cp[i] = mp[s];
}
m = read();
for (reg int i = ; i <= m ; i ++)
{
string s1, s2;
cin >> s1 >> s2;
add(s1), add(s2);
ve[mp[s2]].push_back(mp[s1]);
}
queue <int> q;
for (reg int i = ; i <= tot ; i ++) q.push(i);
while (!q.empty())
{
int x = q.front();q.pop();
for (reg int i = ; i < ve[x].size() ; i ++)
{
int to = ve[x][i];
if (memc[to] > memc[x]) {
memc[to] = memc[x];
q.push(to);
}
}
}
for (reg int i = ; i <= n ; i ++)
ans1 += memc[cp[i]].first, ans2 += memc[cp[i]].second;
printf("%lld %lld\n", ans1, ans2);
return ;
}
[CF467D] Fedor and Essay的更多相关文章
- CF467D Fedor and Essay 建图DFS
Codeforces Round #267 (Div. 2) CF#267D D - Fedor and Essay D. Fedor and Essay time limit per test ...
- Codeforces Round #267 (Div. 2) D. Fedor and Essay tarjan缩点
D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CodeForces 467D(267Div2-D)Fedor and Essay (排序+dfs)
D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #267 Div.2 D Fedor and Essay -- 强连通 DFS
题意:给一篇文章,再给一些单词替换关系a b,表示单词a可被b替换,可多次替换,问最后把这篇文章替换后(或不替换)能达到的最小的'r'的个数是多少,如果'r'的个数相等,那么尽量是文章最短. 解法:易 ...
- 【Codeforces 467D】Fedor and Essay
Codeforces 467 D 题意:给\(m\)个单词,以及\(n\)个置换关系,问将\(m\)个单词替换多次后其中所含的最少的\(R\)的数量以及满足这个数量的最短总长度 思路:首先将置 ...
- Codeforces 467D Fedor and Essay bfs
题目链接: 题意: 给定n个单词. 以下有m个替换方式.左边的单词能变成右边的单词. 替换随意次后使得最后字母r个数最少,在r最少的情况下单词总长度最短 输出字母r的个数和单词长度. 思路: 我们觉得 ...
- CF467 AB 水题
Codeforces Round #267 (Div. 2) (C和D的题解单独写:CF467C George and Job (DP) CF467D Fedor and Essay 建图DFS) C ...
- cf467D(map,vector,bfs,特点提取)
D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #267 (Div. 2)D(DFS+单词hash+简单DP)
D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- jquery easyui dialog一进来直接最大化
扩展自 $.fn.window.defaults.通过 $.fn.dialog.defaults 重写默认的 defaults. 对话框(dialog)是一个特殊类型的窗口,它在顶部有一个工具栏,在底 ...
- Hive导入数据到HBase,再与Phoenix映射同步
1. 创建HBase 表 create 'hbase_test','user' 2. 插入数据 put 'hbase_test','111','user:name','jack' put 'hbase ...
- SpringBoot区块链之以太坊开发(整合Web3j)
最近公司需要ETH兑换功能,ETH转账需要区块打包,这个时候就需要区块检测,目前只是简单整合,后面会将区块自动检测代码上传致QQ群 对于区块链开发不太熟悉的童鞋,可以看看:[区块链开发(零)如何开始学 ...
- /bin/java: 没有那个文件或目录spark/bin/spark-class:行71: /usr/java/jdk1.8
1.检查java环境有没有问题 2.1没问题后检查文件的编码是否有问题
- 跟我学SpringCloud | 第二十章:Spring Cloud 之 okhttp
1. 什么是 okhttp ? okhttp 是由 square 公司开源的一个 http 客户端.在 Java 平台上,Java 标准库提供了 HttpURLConnection 类来支持 HTTP ...
- 深入全面理解面向对象的 JavaScript
深入全面理解面向对象的 JavaScript (原著: 曾 滢, 软件工程师, IBM,2013 年 4 月 17 日) JavaScript 函数式脚本语言特性以及其看似随意的编写风格,导致长期以来 ...
- Scrapy入门操作
一.安装Scrapy: 如果您还未安装,请参考https://www.cnblogs.com/dalyday/p/9277212.html 二.Scrapy基本配置 1.创建Scrapy程序 cd D ...
- Python学习笔记整理总结【Django】:Model操作(二)
1.操作汇总 # 增 # # models.Tb1.objects.create(c1='xx', c2='oo') 增加一条数据,可以接受字典类型数据 **kwargs # obj = models ...
- Maven 梳理-手动创建Maven项目(非web),使用Maven编译、测试、打包、安装、引用
创建空目录 F:\jtDevelop\maventest\myapp 创建pom.xml文件 <project xmlns="http://maven.apache.org/POM/4 ...
- Oracle 查询真实执行计划
什么是真实执行计划 获取Oracle的执行计划,有几种方式.(本文使用Oracle 11g XE版本,以及普通用户scott登录) explain plan for 有两个步骤: explain pl ...