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]= ...
随机推荐
- BroadCastReceiver相关知识--读书笔记
2013-12-30 16:55:07 1. BroadCastReceiver是Android四大组件之一,本质上是一个系统级的监视器. 2. 每次BroadCast事件发生后,系统都会创建对应的B ...
- Java 枚举&注解
枚举类 如何自定义枚举类 JDK1.5之前需要自定义枚举类 JDK 1.5 新增的 enum 关键字用于定义枚举类 若枚举只有一个成员, 则可以作为一种单例模式的实现方式 //枚举类 class Se ...
- getBoundingClientRect() 来获取页面元素的位置
getBoundingClientRect() 来获取页面元素的位置 document.documentElement.getBoundingClientRect 下面这是MSDN的解释: Syn ...
- UTF-8 有BOM和无BOM
BOM(byte order mark)是为 UTF-16 和 UTF-32 准备的,用于标记字节序(byte order).微软在 UTF-8 中使用 BOM 是因为这样可以把 UTF-8 和 AS ...
- iOS对象序列化
系统对象的归档我就不介绍了,这个不复杂,自己看一下就会了. 我在这里主要介绍自定义对象的归档. Sample.h文件 // // Sample.h // Serialization // // ...
- 码表由来:ascll码-Gbk2312-GBK-Unicode-UTF-8
码表ascll码-Gbk2312-GBK-Unicode-UTF-8, ascll是基本的标准码表,GB2312是中文码表,GBK是扩展之后的码表,Unicode是国际通用码表,UTF-8是优化后的U ...
- PAT 07-3 求素数
求素数,这是一个“古老”的问题,每个学过编程的人都应该碰到过,这里是求第M+1到第N个素数,这么经典的问题,当然得给它写上一笔,下面是题设要求及代码实现 /* Name: Copyright: Aut ...
- PED结构获取进程路径和命令行地址
1.FS寄存器 2.进入FS寄存器地址,7FFDD000 3.偏移30为PED结构 4.偏移地址10 3C,44偏移:路径地址,命令行地址 // 通过PEB结构去查找所有进程模块 void *PEB ...
- 事件委托 EventHandler
事件就是当对象或类状态发生改变时,对象或类发出的信息或通知.发出信息的对象或类称为"事件源",对事件进行处理的方法称为"接收者",通常事件源在发出状态改变信息时 ...
- MATLAB简单实现ID3
再看<MATLAB数据分析与挖掘实战>,简单总结下今天看到的经典的决策树算法——ID3. ID3:在决策树的各级节点上,使用信息增益的方法作为属性的选择标准,来帮助确定生成每个节点时所应采 ...