Peter studies the theory of relationaldatabases. Table in the relational database consists of values that arearranged in rows and columns.
There are different normalforms that database may adhere to. Normal forms are designed to minimizethe redundancy of data in the database. For example, a database table for alibrary might have a row for each book and columns for book name, book author,and author's email.
If the same author wrote several books, thenthis representation is clearly redundant. To formally define this kind ofredundancy Peter has introduced his own normal form. A table is in Peter'sNormal Form (PNF) if and only if there is no pair of rows and a pair of columnssuch that the values in the corresponding columns are the same for both rows.
How to compete in ACM ICPC | Peter | peter@neerc.ifmo.ru |
How to win ACM ICPC | Michael | michael@neerc.ifmo.ru |
Notes from ACM ICPC champion | Michael | michael@neerc.ifmo.ru |
The above table is clearly not in PNF, sincevalues for 2rd and 3rd columns repeat in 2nd and 3rd rows. However, if weintroduce unique author identifier and split this table into two tables -- onecontaining book name and author id, and the other containing book id, authorname, and author email, then both resulting tables will be in PNF.
Given a table your task is to figure outwhether it is in PNF or not.
Input contains several datasets. The firstline of each dataset contains two integer numbers n and m ( 1n10000, 1m10), the number of rows andcolumns in the table. The following n lines contain table rows.Each row has m column values separated by commas. Columnvalues consist of ASCII characters from space (ASCII code 32) to tilde (ASCIIcode 126) with the exception of comma (ASCII code 44). Values are not empty andhave no leading and trailing spaces. Each row has at most 80 characters(including separating commas).
For each dataset, if the table is in PNFwrite to the output file a single word ``YES" (without quotes). If thetable is not in PNF, then write three lines. On the first line write a singleword ``NO" (without quotes). On the second line write two integer rownumbers r1 and r2 ( 1r1, r2n, r1r2), onthe third line write two integer column numbers c1 and c2 ( 1c1, c2m, c1c2), so that valuesin columns c1 and c2 arethe same in rows r1 and r2.
How to compete in ACM ICPC,Peter,peter@neerc.ifmo.ru
How to win ACM ICPC,Michael,michael@neerc.ifmo.ru
Notes from ACM ICPC champion,Michael,michael@neerc.ifmo.ru
2,Michael,michael@neerc.ifmo.ru
NO
2 3
2 3
YES
代码:
#include<iostream>
#include<map>
#include<cstdio>
using namespace std;
map<string,int> data;
map<pair<int,int>,int> data1;
int a[10005][11];
int main()
{
int n,m;
string s;
while(cin>>n>>m)
{
int t=0;
cin.get();
for(int i=0; i<n; i++)
{
getline(cin,s);
string x;
int xcount=0;
for(int j=0; j<s.size(); j++)
{
if(s[j]!=',')
{
x+=s[j];
}
if(s[j]==','||j==s.size()-1)
{
if(!data.count(x))
{
data[x]=t;
t++;
}
a[i][xcount++]=data[x];
x="";
}
}
}
int flag=0;
for(int i=0;i<m-1;i++)
{
for(int j=i+1;j<m;j++)
{
for(int k=0;k<n;k++)
{
int x=a[k][i];
int y=a[k][j];
if(data1.count(make_pair(x,y)))
{
printf("NO\n%d%d\n%d %d\n",data1[make_pair(x,y)]+1,k+1,i+1,j+1);
flag=1;
break;
}
data1[make_pair(x,y)]=k;
}
data1.clear();
if(flag) break;
}
if(flag) break;
}
if(!flag) printf("YES\n");
data.clear();
data1.clear();
}
return 0;
}
解析:
题意是:输入一个N行M列的数据库,判断是否存在两行R1,R2两列C1,C2,使得(R1,C1)与(R2,C1)数据相同,且(R1,C2)与(R2,C2)数据相同.若存在输出NO,两个行数,两个列数;不存在输出YES.
思路:
四重循环暴力求解必定超时,所以要降低次数.解决方法:首先读入字符串时,将不同的字符串转换成不同的数字存储(利用map实现).只枚举每两列,对于任意给定的两列,从上至下扫描各行,每次碰到新行,将该行的两列的内容作为一个二元组(pair<int,int>)存取到map(值为该二元组所在的行数)中,利用map中值的唯一性,继续扫描直至发现已经存在的二元组,停止扫描输出NO,该二元组映射的是R1,当前行是R2.