Codeforces Round #285 (Div. 2) ---B. Misha and Changing Handles
来源:互联网 发布:java线程使用 编辑:程序博客网 时间:2024/06/06 03:00
Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user can now change his handle any number of times. But each new handle must not be equal to any handle that is already used or that was used at some point.
Misha has a list of handle change requests. After completing the requests he wants to understand the relation between the original and the new handles of the users. Help him to do that.
The first line contains integer q (1 ≤ q ≤ 1000), the number of handle change requests.
Next q lines contain the descriptions of the requests, one per line.
Each query consists of two non-empty strings old and new, separated by a space. The strings consist of lowercase and uppercase Latin letters and digits. Strings old and new are distinct. The lengths of the strings do not exceed 20.
The requests are given chronologically. In other words, by the moment of a query there is a single person with handle old, and handlenew is not used and has not been used by anyone.
In the first line output the integer n — the number of users that changed their handles at least once.
In the next n lines print the mapping between the old and the new handles of the users. Each of them must contain two strings, old andnew, separated by a space, meaning that before the user had handle old, and after all the requests are completed, his handle is new. You may output lines in any order.
Each user who changes the handle must occur exactly once in this description.
5Misha ILoveCodeforcesVasya PetrovPetrov VasyaPetrov123ILoveCodeforces MikeMirzayanovPetya Ivanov
3Petya IvanovMisha MikeMirzayanovVasya VasyaPetrov123
这一题有点类似赋值的过程,FZUOJ上有一题很类似的题目,解题思路方法几乎一模一样,我的思路使用一个vis数组,将赋值的中间过程标记为访问过,然后再输出。
#include<cstdio>#include<iostream>#include<cstring>using namespace std;char s[1001][30];char s1[1001][30];int main(){ int n; scanf("%d", &n); int ans = 0; char temp[50]; char temp2; int vis[1001]; memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) { scanf("%s%s", s[i],s1[i]); } for (int i = 0; i < n; i++) { strcpy(temp, s1[i]); //vis[i] = 1; for (int j = 0; j < n; j++)//遍历一边 { if (j != i) { if (!vis[j]) { if (strcmp(temp, s[j]) == 0) { strcpy(temp, s1[j]); vis[j] = 1;//标记 ans++; } } } } strcpy(s1[i], temp); } printf("%d\n", n - ans); for (int i = 0; i < n; i++) { if (!vis[i]) { printf("%s %s\n", s[i], s1[i]); } } return 0;}
- Codeforces Round #285 (Div. 2) ---B. Misha and Changing Handles
- Codeforces Round #285 (Div. 2) B. Misha and Changing Handles
- B. Misha and Changing Handles(Codeforces Round 285(div2))
- CodeForces - 501B Misha and Changing Handles
- Codeforces 501 B - Misha and Changing Handles
- CodeForces 501B Misha and Changing Handles
- codeforces 501B Misha and Changing Handles
- CodeForces 501B Misha and Changing Handles
- 【codeforces 510 B Misha and Changing Handles】
- codeforces #285 B题Misha and Changing Handles解题报告
- codeforces#285 A - Contest + B - Misha and Changing Handles
- Misha and Changing Handles CodeForces
- B - Misha and Changing Handles
- B. Misha and Changing Handles
- codeforces 501B B. Misha and Changing Handles
- Codeforces 501B - Misha and Changing Handles (STL)
- CodeForces 501B——Misha and Changing Handles
- CodeForces 501B Misha and Changing Handles(STL map)
- Android中intent启动Activity中intent.setFlags()的作用
- Hibernate 一对多
- sql
- ubuntu14.10 安装oracle jdk
- Python的assert()方法
- Codeforces Round #285 (Div. 2) ---B. Misha and Changing Handles
- Linux Programmer's Manual PROC(5)
- FPGA时序约束的几种方法
- Hibernate lazy属性
- Android 横向列表GridView 实现横向滚动
- 新闻发布系统,网页设计,我们也行
- oracle分页rownum
- Android 分析应用程序占用native内存
- Xcode自动注释插件VVDocumenter和清除Xcode缓存目录的插件