UVa 1592模拟(map)

背景:第一因为找到结果之后没有及时的停止查找而wa了一发,改正后ac。

思路:首先对读入的每一个string,设置一个独特的ID,这样就把string变为int,后来比较的时候就会简化很多,设置ID的时候用map来赋予每一种string对应一个独特的ID。然后构建一个key为pair<int,int>的map,因为行比较多列比较少(列的数为10),就枚举列的所有组合,然后对每组组合来进行map判重。

我的代码;

#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <map>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define LL long long int
using namespace std;
const int M=42,INF=0x3fffffff;
int n,m,diagram[10009][11];

int main(void){
l1:    while(~scanf("%d%d",&n,&m)){
        getchar();
        map<string,int> m1;
        int ID=1;
        for(int i=1;i <= n;i++){
            for(int j=1;j <= m;j++){
                string s1;
                while(true){
                    char c=getchar();
                    if(c == ',' || c == '\n') break;
                    s1.push_back(c);
                }
                //cout << s1 <<endl;
                if(m1.count(s1)) diagram[i][j]=m1[s1];
                else{
                    m1[s1]=ID++;
                    diagram[i][j]=ID-1;
                }
            }
        }
        bool flag=false;
        for(int i=1;i <= m;i++){
            for(int j=i+1;j <= m;j++){
                pair<int,int> p1;
                map<pair<int,int>,int> m2;
                for(int k=1;k <= n;k++){
                    p1.first=diagram[k][i];
                    p1.second=diagram[k][j];
                    if(m2.count(p1)){
                        flag=true;
                        printf("NO\n%d %d\n%d %d\n",m2[p1],k,i,j);
                        goto l1;
                    }else{
                        m2[p1]=k;
                    }
                }
            }
        }
        if(!flag) printf("YES\n");
    }
    return 0;
}
时间: 2024-08-11 05:42:54

UVa 1592模拟(map)的相关文章

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

POJ 3087 Shuffle&#39;m Up (模拟+map)

题目链接:http://poj.org/problem?id=3087 题目大意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块牌归为s1,最顶的c块牌归为s2,依此循环下去. 现在输入s1和s2的初始状态 以及 预想的最终状态s12.问s1 s2经过多少次洗牌之后,最终能达到状态s12,若永远不可能相同,则输出"-1". 解题思路:照着模拟就好了,只是判断是否永远不能达到状态s12需要用map,定义map<

UVA 12096 STL map set 的使用

set这个容器也是STL库的一员,并且在algorithm内直接有 set_union set_intersection  这样求并集交集的操作 map 最方便的地方就是 支持下标访问 举例说明 : 1 #include<iostream> 2 include<cstdio> 3 #include<cstring> 4 #include<vector> 5 #include<map> 6 #include<set> 7 #includ

javacript模拟map输出与去除重复项

1.Javascriptmap输出 function Map(){ // private var obj = {} ;// 空的对象容器,承装键值对 // put 方法 this.put = function(key , value){ obj[key] = value ; // 把键值对绑定到obj对象上 } // size 方法 获得map容器的个数 this.size = function(){ var count = 0 ; for(var attr in obj){ count++;

UVa 512 模拟!

背景:1--wa:最后一组输出不要空行!. 思路:我的思路,就是模拟整个表,把表实行操作之后的形态表示出来,把原表中的数据再在已经模拟的表中去查询.书上的思路是,先把一系列的操作保存在一个结构体数组中,每一个结构体数组元素对应一个操作,最后对于每一个坐标的系统执行这一套操作,就可以得出变化好的坐标!这种方法可能只要知道操作结构体的思想,写起来更容易. 学习:1.写完之后查一遍代码,比去单步调试效果好,输出中间值来调试,效果也很好. 我的代码: #include<stdio.h> #includ

URAL 2002. Test Task(登陆模拟 map )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2002 2002. Test Task Time limit: 0.5 second Memory limit: 64 MB It was an ordinary grim October morning. The sky was covered by heavy gray clouds. It was a little rainy. The rain drops fell on the win

HDU - 3347 Calculate the expression — 模拟 + map存变量

传送门 题意:从输入开始,1.输入样例数:2.然后输入一组样例中的行数n:3.前n-1行为定义变量(之间使用空格隔开),只需要map存进去就可以了(这里有覆盖的情况,故使用mp["s"] = "***"的方法赋值,因为insert的方法如果里面存在的话,插不进入数值):4.然后就是最后一行输入计算式子(之间使用空格隔开). 思路:我使用的字符流的方法分割的的字符串,因为题中说了使用空格隔开的: 变量的储存使用map就可以,在最后一行输入计算式子之后,同样使用字符流分

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 11991 (map vector 嵌套)

其实这题可以直接用vector #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<map> #include<vector> using namespace std; vector<int> str[1000000+100]; int main() { int n,m; int i,j,k; int que,