CodeForces
来源:互联网 发布:温州淘宝运营培训班 编辑:程序博客网 时间:2024/06/11 03:52
题目链接:http://codeforces.com/problemset/problem/501/B点击打开链接
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 handle newis 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 and new, 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
emm这道题一开始先用python写的 后面用c++写了下 发现一些问题
1.不知是不是我理解题意有问题 偶然发现数据只有如同样例那样往后匹配而没有往前匹配
如例子:
5
Misha ILoveCodeforces
Petrov VasyaPetrov123
Vasya Petrov
ILoveCodeforces MikeMirzayanov
Petya Ivanov
之所以发现这个问题 因为我用c++写的代码没有处理第一个字符串的值得存在 上面那个例子处理错误 但是却A了。。
2.用map的问题:键的类型为string 值也为string时 当键不存在时 查询该键对应值为空串 即\0
3.同样出现在查询时的问题 当查询不存在的键值之后 map会为你自动创建这个键为你所查询的键 值为空的一个元素
这个可以再下面的例子中得到验证:
#include <bits/stdc++.h>using namespace std;map<string,string> mmap;int main(){if(mmap["1"]=="")cout << mmap.size() <<endl;}这个的结果是1
这是用c++写的时候发现的问题
相比之下 python似乎更加优雅。。
为了处理c++这个问题 我会的方法只好重头跑一边。。 (不知道怎么处理这种情况) 不过只是增加常数复杂度
下面附两份代码
python:
x=int(input())dic={}s=[]for i in range(x): mid=input() s.clear() s=[a for a in mid.split()] if s[1] in dic: ss=dic[s[1]] dic.pop(s[1]) dic[s[0]]=ss else: flag=0 for key,value in dic.items(): if value==s[0]: dic[key]=s[1] flag=1 if flag==0: dic[s[0]]=s[1]print (len(dic))for key,value in dic.items(): print (key,end=" ") print(value)c++:
#include <bits/stdc++.h>using namespace std;map <string,string> mmap;map <string,string>::iterator it;int main() {int t;cin >> t;while(t--){string a;string b;cin >> a >> b;if(mmap[b]!=""){for(it=mmap.begin();it!=mmap.end();it++){if(it->first==b){mmap.erase(it);break;}}}int flag=0;for(it=mmap.begin();it!=mmap.end();it++){if(it->second==a){it->second=b;flag=1;break;}}if(!flag)mmap[a]=b;}int flag=0;for(it=mmap.begin();it!=mmap.end();it++)if(it->second=="")flag++;cout << mmap.size()-flag << endl;for(it=mmap.begin();it!=mmap.end();it++)if(it->second!="")cout << it->first << " " << it->second << endl;}因为自动增加的关系 只能用flag出去额外增加的个数
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- spring cloud使用Feign实现远程接口的调用
- python: 切片符号(slice notation)
- HTTP 500
- [2017.11.11特辑]以一个光棍节表白案例浅谈ECMAScript6模块化的使用方法
- 软件工程(C语言实践篇)学习心得总结
- CodeForces
- 手机扫描识别证件软件颠覆传统录入
- java 8 lambda 表达式的优缺点总结
- 深入浅出的webpack3入门教程
- 【HDU】5971-Wrestling Match 二分染色
- 依赖注入通俗讲解
- RHEL 6.5系统安装配置图解教程(rhel-server-6.5)
- Python---内存回收机制
- Spring Boot中使用JdbcTemplate访问数据库