uva 1592

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

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <map>
#define BIG 100100
using namespace std;

map<string,int> IDcache;
vector<string> setcache;

map<int,int> ans;
vector<vector<int> >  tab;

int ID(string x){
    if(IDcache.count(x)) return IDcache[x];
    setcache.push_back(x);
    return IDcache[x] = setcache.size() - 1;
} 

int main(){
    int n,m,c1,c2,r,val,tmp,f=0,q;
    string cmd;
    while(cin>>n>>m){
        tab.clear();
        IDcache.clear();
        setcache.clear();
        ans.clear();
        f=0;
        int row = n,col = m;
        getchar();
        while(n--){
            getline(cin,cmd);
            int cnt = 0;
            vector<int> line;
            //cout<<cmd<<endl;
            for(int i=0;i<cmd.size();i++){
                if(cmd[i] == ','){
                    string s = cmd.substr(cnt,i-cnt);
                    cnt = i+1;
                    line.push_back(ID(s));
                    //cout<<s<<":"<<ID(s)<<endl; 
                }
            }
            if(cnt<=cmd.size() ) {string s = cmd.substr(cnt,cmd.size()-cnt+1);line.push_back(ID(s));}
            tab.push_back(line);
        }

        for(c1 = 0;c1< col-1 ;c1++){
            for(c2 = c1+1;c2 < col;c2++){
                val = c1*BIG+c2;
                for(r  = 0;r < row;r++){
                    q = tab[r][c1]*BIG+tab[r][c2];
                    if(ans.count(q)) {tmp = r;f=1;break;}
                    ans[q] = r;
            }
            if(f==1) break;
            ans.clear();
        }
        if(f==1) break;
        }

        if(f==0) cout<<"YES"<<endl;
        else{
            cout<<"NO"<<endl;
            cout<<ans[q]+1<<" "<<tmp+1<<endl;
            cout<<val/BIG+1<<" "<<val%BIG+1<<endl;
        }
    }
    return 0;
}

后来改进的,不需要写一个ID函数那么麻烦

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <map>
#define BIG 100100
using namespace std;

map<string,int> IDcache;
map<int,int> ans;
vector<vector<int> >  tab;

int main(){
    int n,m,c1,c2,r,val,tmp,f=0,q;
    string cmd;
    while(cin>>n>>m){
        tab.clear();
        IDcache.clear();
        ans.clear();
        f=0;
        int t=0,row = n,col = m;
        getchar();
        while(n--){
            getline(cin,cmd);
            string str = "";
            vector<int> line;
            //cout<<cmd<<endl;
            for(int i=0;i<cmd.size();i++){
                if(cmd[i] != ',') str += cmd[i]; 
                if(cmd[i] == ',' || i==cmd.size()-1) {
                    if(!IDcache.count(str)){
                        IDcache[str] = t++; 
                    }
                    line.push_back(IDcache[str]);
                    //cout<<str<<":"<<IDcache[str]<<endl; 
                    str="";
                }
            }
            //if(cnt<=cmd.size() ) {string s = cmd.substr(cnt,cmd.size()-cnt+1);line.push_back(ID(s));}
            tab.push_back(line);
        }


        for(c1 = 0;c1< col-1 ;c1++){
            for(c2 = c1+1;c2 < col;c2++){
                val = c1*BIG+c2;
                for(r  = 0;r < row;r++){
                    q = tab[r][c1]*BIG+tab[r][c2];
                    if(ans.count(q)) {tmp = r;f=1;break;}
                    ans[q] = r;
            }
            if(f==1) break;
            ans.clear();
        }
        if(f==1) break;
        }       
        if(f==0) cout<<"YES"<<endl;
        else{
            cout<<"NO"<<endl;
            cout<<ans[q]+1<<" "<<tmp+1<<endl;
            cout<<val/BIG+1<<" "<<val%BIG+1<<endl;
        }

    }
    return 0;
}

make_pair存二元组:

//声明:map<pair<int,int>,int> ID;
//计算是否出现:ID.count(make_pair(x,y))
//增加一个key:ID[make_pair(x,y)] = i
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值