Codeforces Round #267 (Div. 2) D. Fedor and Essay

D. Fedor and Essay
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.

Input

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.

Output

Print two integers — the minimum number of letters «R» in an optimal essay and the minimum length of an optimal essay.

Sample test(s)
input
3
AbRb r Zz
4
xR abRb
aA xr
zz Z
xr y
output
2 6
input
2
RuruRu fedya
1
ruruRU fedor
output
1 10

本来以为直接dfs就行了,结果wa在了第25组数据上。。

原来还有一种情况不可避免。。

当递归到b之后,因为a的值未确定,所以不能够得到a的正确解。。看来这个错误是完全改不掉了。

网上看了一份代码,用的是优先队列,确实可以避免我的这个问题。。还有,以后要注意如果有过多字符串的题目,不要过多的花空间存储字符串,劲量只存一次,然后保存标号。

wa的代码:

/***********************************************\
 |Author: YMC
 |Created Time: 2014/9/19 22:07:03
 |File Name: d.cpp
 |Description: 
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3f3f3f3f ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;

using namespace std;
#define N 100005
string s;
map<string,vector<string> > ma;
map<string,bool> vis;
map<string,pair<int,int> > mb;
int n,m,len;
string ss[N];
string s1,s2;
inline void get(string& s){
    int len = s.length();
    rep(i,len){
        if(s[i] >= 'A' && s[i] <= 'Z') s[i] -= 'A' - 'a';
    }
} 
inline pair<int,int> minn(pair<int,int> a,pair<int,int> b){
    if(a.first < b.first) return a;
    else if(a.first > b.first) return b;
    else {
        if(a.second <= b.second) return a;
        else return b;
    } 
}
pair<int,int> dfs(string s){
    if(vis[s] == true && mb.count(s) != 0) return mb[s];    //定下来了
    int a = 0,b;
    b = s.length();
    rep(i,b) if(s[i] == 'r') a++;
    pair<int,int> now = make_pair(a,b);
    if(vis[s] == true || ma.count(s) == 0) {
        return now;
    }
    vis[s] = true;
    int size = ma[s].size();
    for(int i=0;i<size;++i){
        now = minn(now,dfs(ma[s][i]));
    }
    mb[s] = now;
    return now;
}
int main() {
	//freopen("input.txt","r",stdin); 
    //ios::sync_with_stdio(false);
    cin>>n;
    rep(i,n){
        cin>>ss[i];
        get(ss[i]);
    }
    cin>>m;
    rep(i,m) {
        cin>>s1>>s2;
        get(s1);get(s2);
        ma[s1].push_back(s2);
    }
    map<string,vector<string> >::iterator it = ma.begin();
    while(it != ma.end()){
        if(vis[it->first] != true){
            dfs(it->first);
        }
        it++;
    }
    ll ans1 = 0,ans2 = 0;
    rep(i,n){
        if(mb.count(ss[i]) == 0){
            int a = 0,b;
            b = ss[i].length();
            rep(j,b) if(ss[i][j] == 'r') a++;
            ans1 += a;
            ans2 += b;
        } else {
            ans1 += mb[ss[i]].first;
            ans2 += mb[ss[i]].second;
        }
    }
    cout<<ans1<<" "<<ans2<<endl;
	return 0;
}

正确的姿势:

#include <bits/stdc++.h>

using namespace std;

map<string, int> Ind;
int wordN;
pair<int, int> r[500005];
int minR[500005];

priority_queue<pair<pair<int, int>, int> > q;
int getInd(string s){
	string t;
	for(int i=0; i < s.size(); ++i){
		if(s[i] >= 'A' && s[i] <= 'Z')
			t.push_back(s[i]+'a'-'A');
		else
			t.push_back(s[i]);
	}
	if(Ind[t])
		return Ind[t];
	Ind[t]=++wordN;
	int rn=0;
	for(int i=0; i < s.size(); ++i)
		if(t[i] == 'r')
			++rn;
	r[Ind[t]]=make_pair(-rn, -t.size());
	q.push(make_pair(make_pair(-rn, -t.size()), Ind[t]));
	return Ind[t];
}

vector<int> adj[500005];
vector<int> essay;
int in[500005];
bool vis[500005];

int main(){
	int m;
	scanf("%d", &m);
	for(int i=0; i < m; ++i){
		string s;
		cin >> s;
		essay.push_back(getInd(s));
	}
	int n;
	scanf("%d", &n);
	for(int i=0; i < n; ++i){
		string a, b;
		cin >> a >> b;
		int A=getInd(a), B=getInd(b);
		adj[B].push_back(A);
	}
		while(!q.empty()){
			int cur=q.top().second;
			q.pop();
			if(vis[cur])
				continue;
			vis[cur]=1;
			for(int j=0; j < adj[cur].size(); ++j){
				int to=adj[cur][j];
				if(r[cur] > r[to]){
					r[to]=r[cur];
					q.push(make_pair(r[to], to));
				}
			}
		}
	long long ansr=0, anslen=0;
	for(int i=0; i < essay.size(); ++i){
		ansr -= r[essay[i]].first;
		anslen -= r[essay[i]].second;
	}
	printf("%I64d %I64d\n", ansr, anslen);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值