Codeforces 467D
D. Fedor and Essaytime limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
InputThe 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 nlines 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.
OutputPrint two integers — the minimum number of letters «R» in an optimal essay and the minimum length of an optimal essay.
Sample test(s)input3
AbRb r Zz
4
xR abRb
aA xr
zz Z
xr youtput2 6input2
RuruRu fedya
1
ruruRU fedoroutput1 10
/*************************************************************************
> File Name: D.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年09月19日 星期五 14时41分44秒
> Propose:
************************************************************************/
#include <map>
#include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
/*Let's fight!!!*/ const int MAX_N = ;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pii;
typedef long long LL; int V; // 顶点数
vector<int> G[MAX_N], rG[MAX_N], vs;
bool used[MAX_N];
int cmp[MAX_N];
//题目变量
map<string, int> HASH;
int n, m, ID[MAX_N], X[MAX_N], Y[MAX_N];
pii s[MAX_N], ss[MAX_N], dp[MAX_N]; void add_edge(int from, int to) {
G[from].push_back(to);
rG[to].push_back(from);
} void dfs(int v) {
used[v] = true;
for (int i = ; i < G[v].size(); i++) {
if (!used[G[v][i]]) dfs(G[v][i]);
}
vs.push_back(v);
} void rdfs(int v, int k) {
used[v] = true;
cmp[v] = k;
ss[k] = min(ss[k], s[v]);
for (int i = ; i < rG[v].size(); i++) {
if (!used[rG[v][i]]) rdfs(rG[v][i], k);
}
} int scc() {
memset(used, false, sizeof(used));
vs.clear();
for (int v = ; v <= V; v++) {
if (!used[v]) dfs(v);
}
memset(used, false, sizeof(used));
int k = ;
for (int i = vs.size() - ; i >= ; i--) {
if (!used[vs[i]]) {
ss[++k] = pii(INF, INF);
rdfs(vs[i], k);
}
}
return k;
} int get(string &str) {
for (int i = ; i < str.size(); i++) {
str[i] = tolower(str[i]);
}
if (HASH.find(str) == HASH.end()) {
HASH[str] = ++V;
s[V].second= str.size();
for (int j = ; j < s[V].second; j++) if (str[j] == 'r') s[V].first++;
return V;
} else {
return HASH[str];
}
} void rebuild() {
for (int i = ; i <= V; i++) G[i].clear();
for (int i = ; i <= m; i++) if (cmp[X[i]] != cmp[Y[i]])
add_edge(cmp[X[i]], cmp[Y[i]]);
} pii DFS(int u) {
if (used[u]) return dp[u];
used[u] = true;
dp[u] = ss[u];
for (int i = ; i < G[u].size(); i++) {
dp[u] = min(dp[u], DFS(G[u][i]));
}
return dp[u];
} int main(void) {
ios::sync_with_stdio(false);
cin >> n;
for (int i = ; i <= n; i++) {
string str;
cin >> str;
int id = get(str);
ID[i] = id;
} cin >> m;
for (int i = ; i <= m; i++) {
string x, y;
cin >> x >> y;
int u = get(x), v = get(y);
add_edge(u, v);
X[i] = u, Y[i] = v;
} int k = scc();
rebuild(); memset(used, false, sizeof(used));
LL resr = , resl = ;
for (int i = ; i <= n; i++) {
int pos = cmp[ID[i]];
DFS(pos);
resr += dp[pos].first;
resl += dp[pos].second;
} cout << resr << ' ' << resl << endl;
return ;
}
Codeforces 467D的更多相关文章
- 【Codeforces 467D】Fedor and Essay
Codeforces 467 D 题意:给\(m\)个单词,以及\(n\)个置换关系,问将\(m\)个单词替换多次后其中所含的最少的\(R\)的数量以及满足这个数量的最短总长度 思路:首先将置 ...
- Codeforces 467D Fedor and Essay bfs
题目链接: 题意: 给定n个单词. 以下有m个替换方式.左边的单词能变成右边的单词. 替换随意次后使得最后字母r个数最少,在r最少的情况下单词总长度最短 输出字母r的个数和单词长度. 思路: 我们觉得 ...
- 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 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- spring:常用的注解
bean.xml中配置依赖 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- opencv-图像遍历
#include "stdafx.h" #include<opencv2/opencv.hpp> #include<iostream> #include&l ...
- filebeat+redis+logstash+elasticsearch+kibana搭建日志分析系统
filebeat+redis+elk搭建日志分析系统 官网下载地址:https://www.elastic.co/downloads 1.下载安装filebeat wget https://artif ...
- 纯CSS样式写刘海屏效果
1. 效果: 2. 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- #、%和$符号在OGNL表达式中的作用
#.%和$符号在OGNL表达式中经常出现,而这三种符号也是开发者不容易掌握和理解的部分.在这里笔者简单介绍它们的相应用途. 1.#符号的用途一般有三种. 1)访问非根对象属性,例如示例中的#ses ...
- nginx使用手册--nginx的命令行参数
nginx的命令行参数 -? 或者 -h 打印命令行参数帮助信息 -c file 为 Nginx 指定一个配置文件,来代替缺省的. -t 不运行,而仅仅测试配置文件.nginx 将检查配置文件的语法的 ...
- Neo4j-Cypher语言语法
Neo4j-Cypher语言语法 梦飞扬 2018-03-15 264 阅读 Neo4j 本文是记录Neo4j图数据库中实用的Cypher语言语法. Cypher是什么 "Cypher&qu ...
- ch5 vlsms
Variabel Length Subnet Mask vlsms 较早的路由协议 ripv1 没有为子网准备的字段,子网信息会被丢失. 这意味着如果一个路由器运行着一个rip协议具有一个确定的子网掩 ...
- HBase实际应用中的性能优化方法
- 【NOIP2018模拟11.01】树
题目 描述 题目大意 维护一个序列,支持三种操作: 1.修改一段区间,将这段区间内的所有数都andandand一个数. 2.询问区间和. 3.询问区间两两相加的平方和. N≤10000N\leq 10 ...