zoukankan      html  css  js  c++  java
  • Database

    U - Database

    Time Limit:9000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

    Appoint description:
     

    Description

    Download as PDF

    Peter studies the theory of relational databases. Table in the relational database consists of values that are arranged in rows and columns.

    There are different normal forms that database may adhere to. Normal forms are designed to minimize the redundancy of data in the database. For example, a database table for a library 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, then this representation is clearly redundant. To formally define this kind of redundancy Peter has introduced his own normal form. A table is in Peter's Normal Form (PNF) if and only if there is no pair of rows and a pair of columns such 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, since values for 2rd and 3rd columns repeat in 2nd and 3rd rows. However, if we introduce unique author identifier and split this table into two tables -- one containing book name and author id, and the other containing book id, author name, and author email, then both resulting tables will be in PNF.

    $	extstyle parbox{.5	extwidth}{
egin{center}
egin{tabular}{vert lvert l...
...hline
Notes from ACM ICPC champion & 2 \
hline
end{tabular}
end{center}}$$	extstyle parbox{.49	extwidth}{
egin{center}
egin{tabular}{vert lvert ...
...ine
2 & Michael & michael@neerc.ifmo.ru \
hline
end{tabular}
end{center}}$

    Given a table your task is to figure out whether it is in PNF or not.

    Input 

    Input contains several datasets. The first line of each dataset contains two integer numbers n and m ( 1$ le$n$ le$10000, 1$ le$m$ le$10), the number of rows and columns in the table. The following n lines contain table rows. Each row has m column values separated by commas. Column values consist of ASCII characters from space (ASCII code 32) to tilde (ASCII code 126) with the exception of comma (ASCII code 44). Values are not empty and have no leading and trailing spaces. Each row has at most 80 characters (including separating commas).

    Output 

    For each dataset, if the table is in PNF write to the output file a single word `` YES" (without quotes). If the table is not in PNF, then write three lines. On the first line write a single word `` NO" (without quotes). On the second line write two integer row numbers r1 and r2 ( 1$ le$r1, r2$ le$n, r1$ 
e$r2), on the third line write two integer column numbers c1 and c2 ( 1$ le$c1, c2$ le$m, c1$ 
e$c2), so that values in columns c1 and c2 are the same in rows r1 and r2.

    Sample Input 

    3 3
    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 3
    1,Peter,peter@neerc.ifmo.ru
    2,Michael,michael@neerc.ifmo.ru
    

    Sample Output 

    NO
    2 3
    2 3
    YES
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <cmath>
     6 #include <string>
     7 #include <vector>
     8 #include <set>
     9 #include <map>
    10 #include <queue>
    11 #include <stack>
    12 #include <sstream>
    13 #include <cctype>
    14 #include <utility>
    15 using namespace std;
    16 const int INF = 0x7fffffff;
    17 const double EXP = 1e-8;
    18 const int MAXR=10005;
    19 const int MAXC=11;
    20 typedef pair<int,int> PII;
    21 int db[MAXR][MAXC];
    22 map<string,int>  mp;
    23 int id,n,m;
    24 int ID(const string &s)
    25 {
    26     if(mp.count(s)==0)
    27         mp[s]=++id;
    28     return mp[s];
    29 }
    30 void solve()
    31 {
    32     for(int c1=0;c1<m;c1++)
    33         for(int c2=c1+1;c2<m;c2++)
    34         {
    35             map<PII,int>  d;
    36             for(int r=0;r<n;r++)
    37             {
    38                 PII p=make_pair(db[r][c1],db[r][c2]);
    39                 if(d.count(p))
    40                 {
    41                     cout<<"NO"<<endl;
    42                     cout<<d[p]+1<<" "<<r+1<<endl;
    43                     cout<<c1+1<<" "<<c2+1<<endl;
    44                     return ;
    45                 }
    46                 d[p]=r;
    47             }
    48         }
    49     cout<<"YES"<<endl;
    50     return ;
    51 }
    52 int main()
    53 {
    54     string s;
    55     while(getline(cin,s))
    56     {
    57         stringstream ss(s);
    58         if(!(ss>>n>>m))
    59             break;
    60         id=0;
    61         mp.clear();
    62         for(int r=0;r<n;r++)
    63         {
    64             getline(cin,s);
    65             int lastpos=-1;
    66             for(int c=0;c<m;c++)
    67             {
    68                 size_t p=s.find(',',lastpos+1);
    69                 if(p==string::npos)
    70                     p=s.length();
    71                 db[r][c]=ID(s.substr(lastpos+1,p-lastpos-1));
    72                 lastpos=p;
    73             }
    74         }
    75         solve();
    76     }
    77     return 0;
    78 }
  • 相关阅读:
    寒假学习进度报告(一)
    【web】公文流转系统制作进度(一)(2019/12/9)
    【规律】A Rational Sequence
    【记忆化搜索】Happy Happy Prime Prime
    【背包问题】PACKING
    【动态规划】正则表达式匹配
    【数据结构】P1310 表达式的值
    【数据结构】P1449 后缀表达式
    【数据结构】P1054 等价表达式
    【数据结构】表达式求值
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4259191.html
Copyright © 2011-2022 走看看