UVa 1592 数据库(c++pair)
Input
Input contains several datasets. The first line of each dataset contains two integer numbersn and m (1n
10000, 1
m
10), the number of rows and columns in the table. The following n lines contain table rows. Each row hasm column values separated by commas. Column values consist of ASCII characters from space (ASCII code 32) to tilde (ASCII code 126) with the exception of comma (ASCII code 44). Values are not empty and have no leading and trailing spaces. Each row has at most 80 characters (including separating commas).
Output
For each dataset, if the table is in PNF write to the output file a single word ``YES" (without quotes). If the table is not in PNF, then write three lines. On the first line write a single word ``NO" (without quotes). On the second line write two integer row numbers r1 andr2 (1r1,r2
n,r1
r2), on the third line write two integer column numbers c1 andc2 (1
c1,c2
m,c1
c2), so that values in columnsc1 andc2 are the same in rowsr1 andr2.
Sample Input
3 3
How to compete in ACM ICPC,Peter,peter@neerc.ifmo.ru
How to win ACM ICPC,Michael,michael@neerc.ifmo.ru
Notes from ACM ICPC champion,Michael,michael@neerc.ifmo.ru
2 3
1,Peter,peter@neerc.ifmo.ru
2,Michael,michael@neerc.ifmo.ru
Sample Output
NO
2 3
2 3
YES
这道题应该使用map来对数据做个一一映射,先将输入的字符串通过map映射成数字,再输入到一个数组里。
之后把c1,c2两列的内容作为一个二元组存到一个map中,由于需要存入两个数据,在这里可以使用pair,在map中就是map<pair<int,int>,int>。
这儿写一下pair的用法:
makr_pair:
pair<int ,int >p (5,6);
pair<int ,int > p1= make_pair(5,6);
pair<string,double> p2 ("aa",5.0);
pair <string ,double> p3 = make_pair("aa",5.0);
有这两种写法来生成一个pair。
如何取得pair的值呢。。
每个pair 都有两个属性值 first 和second
cout<<p1.first<<p1.second;
注意是属性值而不是方法。
#include<iostream>
#include<map>
#include<string> using namespace std; map<string, int> IDcache;
map<pair<int, int>, int> NewIDcache;
int a[][];
int n, m; void match()
{
int x, y;
for (int c1 = ; c1 < m-; c1++)
{
for (int c2 = c1 + ; c2 < m; c2++)
{
for (int r = ; r < n; r++)
{
x = a[r][c1];
y = a[r][c2];
if (!NewIDcache.count(make_pair(x, y))) NewIDcache[make_pair(x, y)] = r;
else
{
cout << "NO" << endl;
cout << NewIDcache[make_pair(x, y)] + << " " << r + << endl;
cout << c1 + << " " << c2 + << endl;
return;
}
}
NewIDcache.clear();
}
}
cout << "YES" << endl;
} int main()
{
while (cin >> n >> m)
{
int t = ;
getchar();
string str;
IDcache.clear();
NewIDcache.clear();
for (int i = ; i < n; i++)
{
int count = ;
str.clear();
getline(cin, str);
int l = str.length();
string s;
for (int j = ; j < l; j++)
{
if (str[j] != ',') s = s + str[j];
if (str[j] == ',' || j == l-)
{
if (!IDcache.count(s)) IDcache[s] = t++;
a[i][count++] = IDcache[s];
s.clear();
}
}
}
match();
}
}
UVa 1592 数据库(c++pair)的更多相关文章
- UVA 10245 The Closest Pair Problem 最近点问题 分治算法
题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...
- UVa 1592 Database(巧用map)
Peter studies the theory of relational databases. Table in the relational database consists of value ...
- UVa - 1592 Database(STL,优化)
给一个n行m列的数据库表格,问有没有两个行 r1,r2 和 c1,c2,满足(r1,r2)的元素=(c1,c2)的元素. n≤10000,m≤10. 直接枚举4个肯定会T的.可以只枚举c1 c2,然后 ...
- UVa 1592 Database (map)
题意:给出n行m列的数据库(数据范围: n 1~10000, m 1~10), 问你能不能找出两行r1, r2,使得这两行中的c1, c2列是一样的, 即(r1,c1)==(r2,c1) && ...
- UVA 10245 - The Closest Pair Problem
Problem JThe Closest Pair ProblemInput: standard inputOutput: standard outputTime Limit: 8 secondsMe ...
- UVA - 1592 Database 枚举+map
思路 直接枚举两列,然后枚举每一行用map依次记录每对字符串出现的是否出现过(字符串最好先处理成数字,这样会更快),如果出现就是"NO",否则就是"YES". ...
- UVA 1592 DataBase
思路: 知识补充: ①make_pair和pair: /*pair是将2个数据组合成一个数据,当需要这样的需求时就可以使用pair,如stl中的map就是将key和value放在一起来保存.另一个应用 ...
- UVa 10245 The Closest Pair Problem (分治)
题意:给定 n 个点,求最近两个点的距离. 析:直接求肯定要超时的,利用分治法,先把点分成两大类,答案要么在左边,要么在右边,要么一个点在左边一个点在右边,然后在左边或右边的好求,那么对于一个在左边一 ...
- Database UVA - 1592
对于每组数据,首先通过一个map将每个字符串由一个数字代替,相同的字符串由相同数字代替,不同的字符串由不同数字代替.那么题目就变为了询问是否存在行r1,r2以及列c1,c2使得str[r1][c1]= ...
随机推荐
- Oracle性能调优
这部分目前主要是从网上搜集来的,后续要在实践中慢慢体会. v$sqltext: 存储的是被分割的sql v$sqlarea: 存储的是完整的sql和一些统计信息,比如累计的执行次数.逻辑读.物理读等( ...
- iOS9/iOS8界面对比 XCode7
Xcde7 bate 无需开发这账号(99¥)可以调试程序 目前是测试版 iOS9/iOS8界面对比 (注:左边为iOS8界面,右边为iOS9界面.) 1.新字体 苹果在 iOS9 中使用旧金山字体取 ...
- poj 1797 Heavy Transportation(最短路径Dijkdtra)
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 26968 Accepted: ...
- wsgi协议
用来为server程序和app/framework程序做连接桥梁的,使server和app/framework各自发展,任意组合 上图是python3.4标准库里面,关于wsgiserver的实现.从 ...
- 读者写者问题继 读写锁SRWLock
在<秒杀多线程第十一篇读者写者问题>文章中我们使用事件和一个记录读者个数的变量来解决读者写者问题.问题虽然得到了解决,但代码有点复杂.本篇将介绍一种新方法--读写锁SRWLock来解决这一 ...
- hadoop的RPC通信
RPC(remote procedure call)远程调用 不同的Java进程间的对象方法调用 一方称作服务端(server),一方称作客户端(client) server端提供对象,供客户端调用, ...
- VMWare Workstation 10.0 Preview CN
What's New in the VMware Workstation Technology Preview July 2013 The VMware Workstation team is exc ...
- hdu2196 树的直径 + bfs
//Accepted 740 KB 15 ms //树的直径 //距离一个顶点最远的点一定是树的直径的一个端点 #include <cstdio> #include <cstring ...
- 创建一个Windows窗体
20140702加: WS_OVERLAPPEDWINDOW这个属性如果写成WS_OVERLAPPED,则窗口没有最大最小按钮以及左边的系统的菜单. vs2010下的代码提示快捷键:CTRL + J ...
- What is hmux in resin?
When I start the Resin server it says hmux listening to localhost:6802 What is this hmux? Is this a ...