[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 ...
随机推荐
- Apollo源码解析-搭建调试环境
准备工作 本地运行时环境 JDK :1.8+ MySQL :5.6.5+ Maven :3.6.1 IDE :IntelliJ IDEA Apollo的表结构对timestamp使用了多个defaul ...
- JAVA中的内存们
我们知道,计算机CPU和内存的交互是最频繁的,内存是我们的高速缓存区,用户磁盘和CPU的交互,而CPU运转速度越来越快,磁盘远远跟不上CPU的读写速度,才设计了内存,用户缓冲用户IO等待导致CPU的等 ...
- 原生JavaScript时间倒计时的方法
这个思路是来源用%的方法来做的: 以前用%做过转秒的 现在用来做倒计时方法: 需要用到的方法是getTime:获取距离1970年1月1日午夜00:00之间的毫秒差: var targetTime=ne ...
- iOS -打包上传成功,在"构建版本"一直刷不出来
今天提交版本到appstore,构建版本一直不出来,等了一天也没有出来,其实就是权限问题,iOS13 来了,所以面临的问题随之而来,苹果给邮箱发了这段话: Dear Developer,We iden ...
- 中国知网(CNKI)验证码识别
中国知网(CNKI)是最重要的中文学术资源数据库,收录绝大多数中文学术刊物.我们可以检索论文,也可以导出检索结果前6000条论文的题录数据. 在CNKI检索结果翻页10次以上,用户需要手动输入验证码才 ...
- Android进程的优先级说明
引言 Android系统尽可能长时间地保持应用程序进程,但为了新建或者运行更加重要的进程,总是需要清除一些进程来回收内存.为了决定保留或终止哪个进程,根据进程内运行的组件及这些组件的状态,系统把每个进 ...
- SpringBoot 2.0 + InfluxDB+ Sentinel 实时监控数据存储
前言 阿里巴巴提供的控制台只是用于演示 Sentinel 的基本能力和工作流程,并没有依赖生产环境中所必需的组件,比如持久化的后端数据库.可靠的配置中心等.目前 Sentinel 采用内存态的方式存储 ...
- [VB.NET Tips]对于基本数据类型的提示
1.类型字符 有时需要直接量后面加上类型字符以明确指定类型,下面把常用的类型字符列出来 类型 字符 Short S Integer I Long L Decimal D Char c Single F ...
- Promise核心原理解析
作者: HerryLo 本文永久有效链接: https://github.com/AttemptWeb...... Promises对象被用于表示一个异步操作的最终完成 (或失败), 及其结果值.主要 ...
- Ubuntu18.04 显卡驱动+Cuda安装踩坑记录 以及Ubuntu虚拟内存的添加
前几天买了张亮机卡,终于把主显卡成功直连到Unraid OS的虚拟机上了.然后就开始安装ubuntu系统开始配置环境,遇到了不少坑,特此记录. gcc版本问题 在安装显卡驱动的时候,不要修改gcc版本 ...