UVA 1592数据库详解 按照紫书上刘汝佳的解题法 利用map和pair(存在多种小细节的改动)
2019-08-12 22:19
197 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_43702096/article/details/99346005
每行都用getline进行输入,不过第一行的数字利用stringstream输入流进行截取,实在是妙啊!
题目链接
本题利用到了一个新的知识点,那就是stl中存在的二元关系pair,详情请看pair当然也可以用结构体进行替换;
#include<iostream> #include<string> #include<map> #include<utility> #include<sstream> using namespace std; typedef pair<int,int>IFF; const int maxr=10000+10; const int maxn=10+5; map<string,int>id; int n,m,cnt; int db[maxr][maxn]; int ID(const string &s) { if(!id.count(s)) id[s]=++cnt; return id[s]; } void find() { for(int c1=0;c1<m;c1++) for(int c2=c1+1;c2<m;c2++) { map<IFF,int>d; for(int r=0;r<n;r++) { IFF p=make_pair(db[r][c1],db[r][c2]);//将第i行的第c1、c2列中字符串所对应的ID临时组成一个pair if(d.count(p))//如果该pair在之前出现过,就说明我们找到了这样的c1,c2,r1,r2 { cout<<"NO"<<endl; cout<<d[p]这里说一下在解决输入问题时,看大lao第二种方法:+1<<" "<<r+1<<endl; cout<<c1+1<<" "<<c2+1<<endl; return; } d[p]=r; } } cout<<"YES"<<endl; } int main() { //主函数 while(cin>>n>>m) { cnt=0; getchar();//注意这里在清空缓冲区时不能用cin.sync(),否则会运行失败;[cin详解](https://blog.csdn.net/selina8921/article/details/79067941) id.clear(); string s; for(int i=0;i<n;i++) { getline(cin,s); int lastpos=0; for(int j=0;j<m;j++) { int location=s.find(',',lastpos); if(location==-1) location=s.size(); db[i][j]=ID(s.substr(lastpos,location-lastpos)); lastpos=location+1; } } find(); } return 0; }
每行都用getline进行输入,不过第一行的数字利用stringstream输入流进行截取,实在是妙啊!
//主函数 int main() { string s; while(getline(cin, s)) { stringstream ss(s); if(!(ss >> n >> m)) break; cnt = 0; id.clear(); for(int i=0;i<n;i++) { getline(cin,s); int lastpos=0; for(int j=0;j<m;j++) { int location=s.find(',',lastpos); if(location==-1) location=s.size(); db[i][j]=ID(s.substr(lastpos,location-lastpos)); lastpos=location+1; } } find(); } return 0; }
相关文章推荐
- 紫书章五例题9 数据库 UVA 1592 (map+vector)
- 紫书章五例题9 数据库 UVA 1592 (map+vector)
- 紫书章五例题9 数据库 UVA 1592 (map+vector)
- UVA ~ 1592 ~ Database (枚举 + map + pair)
- 紫书章五例题9 数据库 UVA 1592 (map+vector)
- 紫书章五例题9 数据库 UVA 1592 (map+vector)
- UVA - 1592 Database map+pair
- 紫书章五例题9 数据库 UVA 1592 (map+vector)
- 紫书章五例题9 数据库 UVA 1592 (map+vector)
- 紫书章五例题9 数据库 UVA 1592 (map+vector)
- UVa 1592 数据库(c++pair)
- UVa 1592 Database ACM解题报告(map和pair的使用)
- 利用ajax提交form表单到数据库详解(无刷新)
- adb命令详解(二)——手机缺失sqlite3时操作数据库的多种解决方案
- 数据库访问优化法则详解之利用更多的资源
- UVA-1592 Datebase 数据库+字符串处理
- UVa 1592 - Database(pair+预处理)
- UVA - 1592 Database 枚举+map
- 刘汝佳紫书 uva201 解题报告
- 数组实战应用及多种解题思路详解