Codeforces 467D - Fedor and Essay (反向建图 + DFS)
2015-01-30 23:39
405 查看
题意
给出原文的几个单词和几组替换词,要求最后包含的r最少,长度越短越好。
思路
因为替换的终点肯定可以确定(R最少的单词),所以可以反向建图,把所有单词按照r排序以后DFS一遍,把它们能走到的点的r和size都更新为自己的r、size。然后直接统计文章中的单词的r和sz
代码
#include <stack>
#include <cstdio>
#include <list>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define BitCount(x) __builtin_popcount(x)
#define BitCountll(x) __builtin_popcountll(x)
#define LeftPos(x) 32 - __builtin_clz(x) - 1
#define LeftPosll(x) 64 - __builtin_clzll(x) - 1
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MAXN = 2e5 + 10;
const int MOD = 29;
const int dir[][2] = { {1, 0}, {0, 1} };
int cases = 0;
typedef pair<int, int> pii;
struct NODE
{
int id, rNum, sz;
bool operator < (const NODE &a) const
{
if (rNum != a.rNum) return rNum < a.rNum;
return sz < a.sz;
}
};
void ChangeToLower(string &str)
{
for (int i = 0; i < SZ(str); i++) str[i] = tolower(str[i]);
}
int CountR(const string &str)
{
int cnt = 0;
for (int i = 0; i < SZ(str); i++)
if (str[i] == 'r') cnt++;
return cnt;
}
map<string, int> oddId;
map<int, int> newId;
int cnt;
vector<NODE> words;
int GetNumber(string &tmp)
{
ChangeToLower(tmp);
if (oddId.count(tmp)) return oddId[tmp];
oddId[tmp] = cnt;
int r = CountR(tmp);
words.PB((NODE){cnt, r, SZ(tmp)});
cnt++;
return cnt - 1;
}
vector<int> G[MAXN], article;
int vis[MAXN];
void DFS(int curId, int r, int sz)
{
vis[curId] = 1;
for (int i = 0; i < SZ(G[curId]); i++)
{
int tmpId = G[curId][i];
if (!vis[tmpId])
{
vis[tmpId] = 1;
words[newId[tmpId]].rNum = r;
words[newId[tmpId]].sz = sz;
DFS(tmpId, r, sz);
}
}
}
int main()
{
//ROP;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
string tmp;
cin >> tmp;
int curNumber = GetNumber(tmp);
article.PB(curNumber);
}
int ncnv;
cin >> ncnv;
for (int i = 0; i < ncnv; i++)
{
string tmp;
cin >> tmp;
int u = GetNumber(tmp);
cin >> tmp;
int v = GetNumber(tmp);
G[v].PB(u);
}
sort(words.begin(), words.end());
for (int i = 0; i < SZ(words); i++) newId[words[i].id] = i;
for (int i = 0; i < SZ(words); i++)
if (!vis[words[i].id]) DFS(words[i].id, words[i].rNum, words[i].sz);
LL rAns = 0, szAns = 0;
for (int i = 0; i < SZ(article); i++)
rAns += words[newId[article[i]]].rNum, szAns += words[newId[article[i]]].sz;
cout << rAns << " " << szAns << endl;
return 0;
}
相关文章推荐
- Codeforces Round #267 Div.2 D Fedor and Essay -- 强连通 DFS
- Codeforces Round #267 Div.2 D Fedor and Essay -- 强连通 DFS
- codeforces 780-C. Andryusha and Colored Balloons(dfs)
- CodeForces - 580C Kefa and Park (dfs)
- Codeforces 510B:Fox And Two Dots(DFS变形+技巧)
- CodeForces 682C Alyona and the Tree (树上DFS)
- 【Codeforces 781 A Andryusha and Colored Balloons】+ DFS
- 【CODEFORCES】 B. Fedor and New Game
- CodeForces 780C Andryusha and Colored Balloons【DFS】
- Codeforces-877E:Danil and a Part-time Job(DFS序列+线段树)
- Codeforces Round #267 (Div. 2) D. Fedor and Essay
- codeforces-510C-Fox And Names【DFS】
- Codeforces 754 D Fedor and coupons
- 【codeforces 754D】Fedor and coupons
- Codeforces - 277.5 (Div. 2)C - Given Length and Sum of Digits...(模拟 or dfs)
- Fedor and coupons CodeForces - 754D (优先队列,贪心)
- codeforces 510.B Fox And Two Dots (DFS好题)
- codeforces 763A Timofey and a tree(DFS)
- codeforces 842C. Ilya And The Tree(dfs)
- CodeForces - 842C Ilya And The Tree 树上DFS