1592 - Database (UVA)

题目链接如下:

Online Judge

这道题刘汝佳的解法复杂度要低很多。注意到m远小于n,他的解法是遍历不同的列组合c1, c2, 然后再遍历行,如果对应元素相同,输出。

我的解法复杂度高很多,但这道题的时间限制有9秒,所以能AC.....

cpp 复制代码
#include <iostream>
#include <cstdio>
#include <string>
#include <map>
#include <set>
#include <algorithm>
// #define debug
const int maxN = 10001;
const int maxM = 11;
const int comma = 44;

int n, m, pos, pre, cnt = 0;
int table[maxN][maxM];
std::string line, str;
std::map<std::string, int> mp;
char ch;
bool flag;

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while(scanf("%d %d", &n, &m) == 2){
        getchar();
        for(int i = 1; i <= n; ++i){
            getline(std::cin, line);
            line.push_back(',');
            pre = -1;
            for(int j = 1; j <= m; ++j){
                pos = line.find(',', pre + 1);
                str = line.substr(pre + 1, pos - pre - 1);
                pre = pos;
                if(mp.find(str) == mp.end()){
                    mp[str] = ++cnt;
                }
                table[i][j] = mp[str];
            }
        }
        flag = true;
        for(int i = 1; i < n; ++i){
            for(int j = i + 1; j <= n; ++j){
                std::set<int> st;
                for(int k = 1; k <= m; ++k){
                    if(table[i][k] == table[j][k]){
                        st.insert(k);
                    }
                }
                if(st.size() > 1){
                    printf("NO\n%d %d\n%d %d\n", i, j, *(st.begin()), *(++st.begin()));
                    flag = false;
                    i = n;
                    break;
                }
            }
        }
        printf("%s", flag ? "YES\n" : "");
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

根据刘汝佳解法改写的代码如下,还是快不少的:

cpp 复制代码
#include <iostream>
#include <cstdio>
#include <string>
#include <map>
#include <algorithm>
// #define debug
const int maxN = 10001;
const int maxM = 11;
const int comma = 44;
const int hashMul = 100001;

int n, m, pos, pre, cnt = 0;
int table[maxN][maxM];
std::string line, str;
std::map<std::string, int> mp;
char ch;
bool flag;

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while(scanf("%d %d\n", &n, &m) == 2){
        mp.clear();
        for(int i = 1; i <= n; ++i){
            getline(std::cin, line);
            line.push_back(',');
            pre = -1;
            for(int j = 1; j <= m; ++j){
                pos = line.find(',', pre + 1);
                str = line.substr(pre + 1, pos - pre - 1);
                pre = pos;
                if(mp.find(str) == mp.end()){
                    mp[str] = ++cnt;
                }
                table[i][j] = mp[str];
            }
        }
        flag = true;
        for(int i = 1; i < m; ++i){
            for(int j = i + 1; j <= m; ++j){
                std::map<int, int> p;
                for(int k = 1; k <= n; ++k){
                    int temp = table[k][i] * hashMul + table[k][j];
                    if(p.count(temp)){
                        printf("NO\n%d %d\n%d %d\n", p[temp], k, i, j);
                        flag = false;
                        j = m + 1;
                        i = m + 1;
                        break;
                    } else{
                        p[temp] = k;
                    }
                }
            }
        }
        printf("%s", flag ? "YES\n" : "");
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}
相关推荐
Once_day36 分钟前
Linux错误(6)X64向量指令访问地址未对齐引起SIGSEGV
linux·c++·sse·x64·sigsegv·xmm0
JhonKI1 小时前
【从零实现Json-Rpc框架】- 项目实现 - 客户端注册主题整合 及 rpc流程示意
c++·qt·网络协议·rpc·json
__lost1 小时前
为什么new分配在堆上,函数变量在栈上+递归调用时栈内存的变化过程
c++·内存分配
序属秋秋秋1 小时前
算法基础_基础算法【位运算 + 离散化 + 区间合并】
c语言·c++·学习·算法·蓝桥杯
jyyyx的算法博客2 小时前
【再探图论】深入理解图论经典算法
c++·算法·图论
念_ovo2 小时前
【算法/c++】利用中序遍历和后序遍历建二叉树
数据结构·c++·算法
Vitalia2 小时前
⭐算法OJ⭐寻找最短超串【动态规划 + 状态压缩】(C++ 实现)Find the Shortest Superstring
开发语言·c++·算法·动态规划·动态压缩
C-DHEnry3 小时前
迪杰斯特拉+二分+优先队列+拓扑+堆优化(奶牛航线Cowroute、架设电话线dd、路障Roadblocks、奶牛交通Traffic)
c++·算法·动态规划·二分·拓扑·堆优化·迪杰斯特拉
这个懒人3 小时前
H.264编码解析与C++实现详解
c++·ffmpeg·h264
努力学习的小廉3 小时前
【C++11(中)】—— 我与C++的不解之缘(三十一)
android·java·c++