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;
}
相关推荐
郝学胜-神的一滴23 分钟前
Linux系统函数link、unlink与dentry的关系及使用注意事项
linux·运维·服务器·开发语言·前端·c++
赵杰伦cpp25 分钟前
list的迭代器
开发语言·数据结构·c++·算法·链表·list
老歌老听老掉牙1 小时前
使用 OpenCASCADE 提取布尔运算后平面图形的外轮廓
c++·平面·opencascade
闻缺陷则喜何志丹1 小时前
【动态规划】数位DP的原理、模板(封装类)
c++·算法·动态规划·原理·模板·数位dp
胖咕噜的稞达鸭1 小时前
二叉树搜索树插入,查找,删除,Key/Value二叉搜索树场景应用+源码实现
c语言·数据结构·c++·算法·gitee
进击的大海贼2 小时前
QT-C++ 自定义加工统计通用模块
开发语言·c++·qt
lingran__2 小时前
算法沉淀第四天(Winner)
c++·算法
清风wxy2 小时前
C语言基础数组作业(冒泡算法)
c语言·开发语言·数据结构·c++·windows·算法
我是华为OD~HR~栗栗呀2 小时前
华为OD-21届考研-Java面经
java·前端·c++·python·华为od·华为·面试
nnerddboy3 小时前
QT(c++)开发自学笔记:2.TCP/IP
c++·笔记·qt