字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles

题目传送门

 1 /*
 2     题意:给出一系列名字变化,问最后初始的名字变成了什么
 3     字符串处理:每一次输入到之前的找相印的名字,若没有,则是初始的,pos[m] 数组记录初始位置
 4                 在每一次更新时都把初始pos加上去,那么就保证更新了初始的名字,这也是唯一要思考的地方了:)
 5 */
 6 #include <cstdio>
 7 #include <iostream>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <cmath>
11 #include <string>
12 #include <map>
13 #include <ctime>
14 using namespace std;
15
16 const int MAXN = 1e3 + 10;
17 const int INF = 0x3f3f3f3f;
18 string old[MAXN];
19 string change[MAXN];
20 string ans[MAXN];
21 int pos[MAXN];
22
23 int main(void)        //Codeforces Round #285 (Div. 2) B. Misha and Changing Handles
24 {
25     freopen ("B.in", "r", stdin);
26
27     int n;
28     while (scanf ("%d", &n) == 1)
29     {
30         memset (pos, 0, sizeof (pos));
31         string tmp;    int m = 0;
32         for (int i=1; i<=n; ++i)
33         {
34             cin >> tmp; cin >> change[i];
35             bool ok = false;
36             for (int j=1; j<i; ++j)
37             {
38                 if (tmp == change[j])
39                 {
40                     ok = true;
41                     ans[pos[j]] = change[i];
42                     pos[i] = pos[j];    break;
43                 }
44             }
45             if (!ok)
46             {
47                 old[++m] = tmp; ans[m] = change[i];    pos[i] = m;
48             }
49         }
50
51         printf ("%d\n", m);
52         for (int i=1; i<=m; ++i)
53         {
54             cout << old[i] << " " << ans[i] << endl;
55         }
56     }
57
58     // cout << "time: " << (double) clock () / CLOCKS_PER_SEC * 1000 << " ms\n";
59
60     return 0;
61 }
时间: 2024-08-09 02:18:37

字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles的相关文章

图论/位运算 Codeforces Round #285 (Div. 2) C. Misha and Forest

题目传送门 1 /* 2 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 3 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 4 关键在于更新异或和,精髓:a ^ b = c -> a ^ c = b, b ^ c = a; 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <cmath> 9 #include <algorith

Codeforces Round #285 (Div. 2) C - Misha and Forest

思路:类似一个拓扑排序的题,根据 对度数为1的点,它所连的边的编号即为异或和这一 规律,直接进行拓扑排序即可. 每次对得到的节点度数减一,并且异或上他所连的节点. 代码: #include<cstdio> #include<cstring> #include<iostream> #include<vector> #include<algorithm> #include<queue> using namespace std; typed

字符串处理 Codeforces Round #305 (Div. 2) A. Mike and Fax

题目传送门 1 /* 2 字符串处理:回文串是串联的,一个一个判断 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 #include <algorithm> 8 #include <string> 9 using namespace std; 10 11 const int MAXN = 1e3 + 10; 12 const int INF = 0x3f3

Codeforces Round #285 (Div. 2) (A、B、C、D)

A:就根据题意计算比较一下即可 B:从每个起点往后走一遍走到底,输出即可,字符串直接map映射掉 C:类似拓扑排序,从临接个数为1的入队,那么临接Xor和,其实就是他的上一个结点,因为他只临接了一个结点,这样利用拓扑排序,当一个结点的度数为1的时候入队即可,注意要判断一下度数0的情况,直接continue D:利用树状数组去求这种大的全排列数,其实一个全排列 ,可以看成a1 * (n - 1)! + a2 * (n - 2)!....,那么其实只要处理出每一项的系数,然后在由系数就可以求出变换后

Codeforces Round #285 (Div. 2) ABCD

A题 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 7 using namespace std; 8 9 #define LL long long 10 #define eps 1e-8 11 #define inf 0x3f3f3f3f 12 #define mnx 100010

CodeForces Round #285 Div.2

C.Misha and Forest (图论 BFS) 比赛进行了一半才想起来有场CF没打,=_=|| 前两道题快速切掉,C题一直卡没什么好的思路 憋了几天,忍不住偷偷瞄了一下别人AC的代码,发现我题没看清题目,题中说了给出的图是森林. 于是切入点找到了! 题意: 一个由n个节点构成的森林,编号从0到n-1,给出每个节点的 度数 和 相邻节点编号的异或和,输出图中的边数和所有邻接节点的编号. 分析: 因为是森林,所以图中的叶子节点就是突破口.叶子节点最明显的特征就是: 度数为1 它相邻节点编号的

Codeforces Round #285 (Div.1 B &amp; Div.2 D) Misha and Permutations Summation --二分+树状数组

题意:给出两个排列,求出每个排列在全排列的排行,相加,模上n!(全排列个数)得出一个数k,求出排行为k的排列. 解法:首先要得出定位方法,即知道某个排列是第几个排列.比如 (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0). 拿排列(1,2,0)来说,首位是1,前面有cnt=1个小于1的没被用过的数(0),所以它的排行要加上(cnt=1)*2!,第二位为2,因为1已经放了,所以小于2的只有0了,即cnt=1个,所以,排

【树链剖分】【dfs序】【LCA】【分类讨论】Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground

一棵树,q次询问,每次给你三个点a b c,让你把它们选做s f t,问你把s到f +1后,询问f到t的和,然后可能的最大值是多少. 最无脑的想法是链剖线段树--但是会TLE. LCT一样无脑,但是少一个log,可以过. 正解是分类讨论, 如果t不在lca(s,f)的子树内,答案是dis(lca(s,f),f). 如果t在lca(s,f)的子树内,并且dep(lca(s,t))>dep(lca(f,t)),答案是dis(lca(s,t),f): 否则答案是dis(lca(f,t),f). #in

Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground

题意:给出 一颗树,然后q个询问,每个询问给出3个点,让我们自己选择一个起点一个终点,这条路线上的点标记,问第三个点到终点的最多标记点是多少 思路:第三个点到终点的标记点,肯定是第三个点与起点的最近公共祖先到终点的标记点.我们可以求出三个点的的三种最近公共祖先,取深度最大的点K,然后求K到三个点的距离+1 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=1e5+10; 4 5 const int DEG=17; 6