UVA-1592 Database

第一篇文章,以后会坚持写的。
这道题目就是找r1,r2,c1,c2使得(r1,c1)和(r2,c1)相等,(r1,c2)和(r2,c2)相等
对效率有一定要求,不小心就会TLE
最初的算法是枚举r1,r2然后横向扫描,时间复杂度为Θ(n^3)无疑TLE。

后来经过改进将每个string与处映射成一个数,枚举c1,c2然后从上往下扫描将(r,c1)(r,c2)字符串对应的两个整数
组合成一个结构体作为map的键值,用r作为value,若键值已存在就为所求。
注意尽量将string映射成int的值存在一个二维数组中,map查找也是要时间的。
映射的值也尽量在输入中计算,否则单独计算也是要Θ(n^2)时间

最后勉勉强强AC了吧,不过效率还是有待改进,rank1 0.2几秒就过了,有空还会改的。
附代码

 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<map>
 5 #include<cstdio>
 6 using namespace std;
 7 struct kv
 8 {
 9     int k;
10     int v;
11     kv(int a,int b){
12         k=a;v=b;
13     }
14 };
15 bool operator < (const kv& a,const kv& b){
16     if(a.k<b.k)return 1;
17     if(a.k>b.k)return 0;
18
19     return a.v<b.v;
20 }
21 int main()
22 {
23     //ios::sync_with_stdio(false);
24
25     int m,n;
26     while(cin>>n>>m){
27         getchar();
28         char line[100];
29         string s[10020][11];
30         int a[n][m];
31         map<string,int> con;
32         int num=1;
33
34         for(int i=0;i<n;i++){
35             gets(line);
36             int c=0;
37             int len=strlen(line);
38             for(int j=0;j<len;j++){
39                 if(line[j]==‘,‘){
40                     c++;
41                     if(!con.count(s[i][c-1])){
42                         con[s[i][c-1]]=num++;
43                         a[i][c-1]=num-1;
44                     }
45                     else a[i][c-1]=con[s[i][c-1]];
46                 }
47                 else s[i][c].push_back(line[j]);
48             }
49             if(!con.count(s[i][c])){
50                 con[s[i][c]]=num++;
51                 a[i][c]=num-1;
52             }
53             else a[i][c]=con[s[i][c]];
54         }
55
56         int sign=0;
57         for(int c1=0;c1<m;c1++){
58             for(int c2=c1+1;c2<m;c2++){
59                 map<kv,int> database;
60                 for(int i=0;i<n;i++){
61                     kv k(a[i][c1],a[i][c2]);
62                     if(database.count(k)){
63                         sign=1;
64                         cout<<"NO"<<endl;
65                         cout<<database[k]<<" "<<i+1<<endl;
66                         cout<<c1+1<<" "<<c2+1<<endl;
67                         goto ssss;
68                     }
69                     else {
70                         database[k]=i+1;
71                     }
72                 }
73             }
74         }
75         ssss:;
76         if(sign==0)cout<<"YES"<<endl;
77     }
78     return 0;
79 }
时间: 2024-08-08 15:57:29

UVA-1592 Database的相关文章

UVa 1592 Database (map)

题意:给出n行m列的数据库(数据范围: n 1~10000, m 1~10), 问你能不能找出两行r1, r2,使得这两行中的c1, c2列是一样的, 即(r1,c1)==(r2,c1) && (r1,c2)==(r2,c2), 可以的话输出NO并且输出r1, r2, c1, c2, 否则输出YES! 分析:如果是四个for循环去枚举全部的r1,r2,c1,c2复杂度是O(n*n*m*m),肯定超时!能否减少枚举量?或者只是枚举行或者列?这里可以使用map做到!map的键值设置为一个pai

UVa - 1592 Database(STL综合,强推!)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51293 #include <iostream> #include <map> #include <vector> #include <string> #include <cstdio> #include <cstring> #define MAXN 10005 #define MAXM 15 using

uva 1592 Database (STL)

题意: 给出n行m列共n*m个字符串,问有没有在不同行r1,r2,有不同列c1,c2相同.即(r1,c1) = (r2,c1);(r1,c2) = (r2,c2); 如 2 3 123,456,789 123,654,789 (1,3) 就对应(3,3) 如果有这种对应,就输出NO,然后输出两个行号, 两个列号.否则输出YES. 分析: 将每个字符串映射成一个值. 枚举任意两列,构成一对pair,枚举任意两行是否相等,任意两列组合C(10,2) = 45种 行最多有10000行. 其实这种方法很

【UVa 1592】Database

Peter studies the theory of relational databases. Table in the relational database consists of values that are arranged in rows and columns. There are different normal forms that database may adhere to. Normal forms are designed to minimize the redun

1592 - Database

Peter studies the theory of relational databases. Table in the relational database consists of values that are arranged in rows and columns. There are different normal forms that database may adhere to. Normal forms are designed to minimize the redun

UVa 1592 数据库(c++pair)

Input Input contains several datasets. The first line of each dataset contains two integer numbersn and m (1n10000, 1m10), the number of rows and columns in the table. The following n lines contain table rows. Each row hasm column values separated by

UVa 1592模拟(map)

背景:第一因为找到结果之后没有及时的停止查找而wa了一发,改正后ac. 思路:首先对读入的每一个string,设置一个独特的ID,这样就把string变为int,后来比较的时候就会简化很多,设置ID的时候用map来赋予每一种string对应一个独特的ID.然后构建一个key为pair<int,int>的map,因为行比较多列比较少(列的数为10),就枚举列的所有组合,然后对每组组合来进行map判重. 我的代码: #include <set> #include <stack&g

uva 1592

一开始写的是这样: 用了书上写的ID函数,然后存二元组使用的大数相乘的方法,因为看错题目BIG一开始定义为10010,错了好几次找了半天错误=.= 后来发现存二元组也可以用make_pair(x,y) #include <cstdio> #include <cstring> #include <iostream> #include <vector> #include <map> #define BIG 100100 using namespace

UVa 1592 数据库

题目 给出一个表格,问有没有r1.r2.c1.c2满足着两行与两列相交的单元格内部分别相同. 题解 #include <iostream> #include <string> #include <map> #include <set> #include <vector> #include <algorithm> using namespace std; const int COL = 10 + 5; const int ROW = 1

UVa第五章STL应用 习题((解题报告))详细!

例题5--9 数据库 Database UVa 1592 <strong><span style="font-size:18px;"><span style="font-size:18px;"><strong><span style="font-size:18px;">#include<iostream> #include<string> #include<