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 ;
}
最新文章
- First commit
- cursor
- (二)miller指导查看主控板寄存器操作
- html中不要忽略一些细节
- trap在shell中捕捉信号
- Windows Phone 离主流系统还很远
- JavaIO(04)字符流--Writer and Reader
- Perl数组: shift, unshift, push, pop
- SpringMVC(三)——其他知识
- 读书笔记 effective c++ Item 50 了解何时替换new和delete 是有意义的
- radis学习总结
- JAVA基础1——字节&;位运算
- springMVC学习总结(四)springmvc处理json数据类型以及fastjson的使用
- Microsoft Power BI 学习笔记
- 二、Windows Server 2016 AD 组织单位、组、用户的创建
- JDBC的使用-----Statement
- 【译】异步JavaScript的演变史:从回调到Promises再到Async/Await
- 七牛免费SSL证书申请全流程
- MUI - 复选框、单选框、使用js获取选择值
- 使用X-UA-Compatible来设置IE8兼容模式
热门文章
- windows10 vs2019 + opencv 3.4.7环境搭建
- 简单搭建 @vue-cli3.0 及常用sass使用
- 今天给各位介绍Linux touch命令详述
- 完全卸载之前8.0的Mysql,安装5.5mysql
- Cannot find module &#39;@babel/plugin-proposal-class-properties&#39;
- git 命令行(四)-推送分支到远程
- Android HttpClient 用法以及乱码解决
- sql不用拼接语句实现动态查询条件
- Bash Specially-crafted Environment Variables Code Injection Vulnerability Analysis
- quatz调度-手动终止线程(1) 创建触发器,线程执行完毕后添加到cleaner list