Uva 1592 Database

Uva 1592 Database 

题目大意:给你一个储存n*m个元素的数据库,让你查询是否存在两行R1,R2。第R1行和第R2行的两行中的第C1和C2的元素分别相同。如果有相同,则输出NO然后把R1 C1

R2 C1 输出,如果没有,就输出YES。

刚拿到这道题第一反应就是枚举,但是枚举效率实在是太低了,四重循环,肯定超时,那要怎么办呢,就缩成三重,枚举每一行的C1,C2弄成pair存到map里(map记录的是这个pair所在的层数),下一层的时候就和继续枚举C1,C2并且在map里查询这个c1,c2是否存在,如果存在,就输出no然后取出map里的东西输出就好了。

关于字符串处理,直接getline到string里,然后把‘,’替换成空格扔到stringsteam里读取,会比我代码写的要好写很多。我是一个一个读的。不太好写。

接下来上代码,70行就搞定了,还是挺简单的。不过估计还有更高效的写法。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <map>
#include <vector>
using namespace std;

map<pair<string,string>,int> die;
pair<string,string> t;
int Id=0;
int bh(int n)
{
	//cout<<t.first<<"                  "<<t.second<<endl;
	if(!die.count(t))
	{
		die[t]=n;
		return -1;
	}
	else
	{
		return die[t];
	} 
}
int main()
{
	//freopen("data.txt","w",stdout);
	int m,n;
	char c;
	while(cin>>n>>m)
	{
		string dat[10005][10];
		getchar();
		for(int i=0;i<n;i++)
		{
			int d=0;	
			while((c=getchar())&&c!='\n')
			{
				if(c==',')
				{
					d++;
					continue;
				}
				dat[i][d].push_back(c);
			}
		}
		for(int i=0;i<m;i++)
		{
			for(int j=i+1;j<m;j++)
			{
				die.clear();
				for(int h=0;h<n;h++)
				{
					t.first=dat[h][i];
					t.second=dat[h][j];
					int f=bh(h);
					if(f!=-1)
					{
						cout <<"NO"<<endl;
						cout <<f+1<<" "<<h+1<<endl;
						cout <<i+1<<" "<<j+1<<endl;
						goto end;
					}
				}
			}
		}
		printf("YES\n");
		end:;
	}
	return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值