BUPT复试专题—数据库检索(2014软院)

题目描述

在数据库的操作过程中,我们进场会遇到检索操作。这个题目的任务是完成一些特定格式的检索,并输出符合条件的数据库中的所有结果。 
我们现在有一个数据库,维护了学生的姓名(Name),性别(Sex)以及出生日期(Birthday)。其中,Name项是长度不超过30的字符串,只可能包含大小写字母,没有空格;Sex项进可能为‘Male’或者‘Female’(不含引号);Birthday项以yyy/mm/dd的格式存储,如:1990/01/01, 
1991/12/31,等等。 
每个查询所可能包含的条件如下: 
Name=‘REQUIRED_NAME’,查询姓名为REQUIRED_NAME的学生,其中REQUIRED_NAME为长度在1到30之间的字符串; 
Sex=‘Male’或Sex=‘Female’,查询性别为男/女的学生; 
Birthday=‘yyy/mm/dd’,查询出生年/月/日为特定值的学生。如果其中某项为’’,则说明该项不受限制。例如,‘1990/06/’表示1990年6月出生,‘/03/’表示出生月份为3月。 
给定数据库的所有表项以及若干条查询,你需要对每条查询输出它返回的结果。 

输入

输入包含多组测试数据。输入的第一行为测试数据的组数T(1<=T<=50)。 
对于每组测试数据,第一行是两个整数N和M(N,M<=500),分别表示数据的数量以及查询的数量。 
接下来N行,每行以Name Sex Birthday的形式给出每个学生的信息。 
没下来M行,每行给出若干条限制条件,以空格隔开。条件以Name Sex Birthday的顺序给出(如果存在),且每种限制条件最多只出现一次。 

输出

对于每条查询,按照输入的顺序输出符合条件的学生姓名,每个一行。如果没有符合查询的信息,则输出一行NULL。

样例输入

1
5
6 
Michael Male 1990/02/28
Amy Female 1992/04/03
Tom Male 1991/12/15
Lynn Female 1991/04/09
Zheng Male 1990/04/20
Name='Amy'
Name='Betty'
Sex='Female' Birthday='*/04/09'
Sex='Female' Birthday='*/*/*'
Name='Michael' Sex='Female'
Name='Michael' Sex='Male' Birthday='1990/02/*'

样例输出

Amy 
NULL 
Lynn 
Amy 
Lynn 
NULL 
Michael

来源

2014软院D题 

。。。。。TLE了,具体原因是string用得太多,应改为char数组,TLE的版本。。。。↓

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
using namespace std;
struct dog
{
    string sex;
    string birthday;
};
map<string,dog> donser;
bool deal(string a,string b)
{
    string temp="/";
    int pos1=a.find(temp[0]);
    int pos2=b.find(temp[0]);
    if(pos1==pos2)
    {
        string s1=a.substr(0,pos1);
        string s2=b.substr(0,pos2);
        if(s1!=s2)
            return false;
    }
    a.erase(0,pos1+1);
    b.erase(0,pos2+1);
    pos1=a.find(temp[0]);
    pos2=b.find(temp[0]);
    if(pos1==pos2)
    {
        string s1=a.substr(0,pos1);
        string s2=b.substr(0,pos2);
        if(s1!=s2)
            return false;
    }
    a.erase(0,pos1+1);
    b.erase(0,pos2+1);
    if(a.size()==b.size())
    {
        if(a!=b)
            return false;
    }
    return true;
}
int main()
{
    int tes,m,n;
    //freopen("test.out","w",stdout);
    while(~scanf("%d",&tes))
    {
        while(tes--)
        {
            cin>>m>>n; 
            while(m--)
            {
                string a,b,c;
                cin>>a>>b>>c;
                donser[a].sex=b;
                donser[a].birthday=c;
            }
            cin.get();
            while(n>0)
            {
                n--;
                string str,temp="a'a",c_name="",c_sex="",c_birth="";
                bool lable=false,named=false,sexd=false,birthd=false;
                getline(cin,str);
                if(str[0]=='N')
                {
                    str.erase(0,6);
                    int pos=str.find(temp[1]);
                    c_name=str.substr(0,pos);
                    named=true;
                    str.erase(0,pos+1);
                    if( donser.find(c_name)==donser.end())
                    {
                        cout<<"NULL"<<endl;
                        continue;
                    }
                    if(str.size()>0)
                        str.erase(0,1);
                }
                if(str.size()>0&&str[0]=='S')
                {
                    str.erase(0,5);
                    int pos=str.find(temp[1]);
                    c_sex=str.substr(0,pos);
                    sexd=true;
                    str.erase(0,pos+1);
                    if(str.size()>0)
                        str.erase(0,1);
                }
                if(str.size()>0&&str[0]=='B')
                {
                    str.erase(0,10);
                    int pos=str.find(temp[1]);
                    c_birth=str.substr(0,pos);
                    birthd=true;
                    str="";
                }
                if(named)
                {
                    if((donser[c_name].sex==c_sex||sexd==false)&&(deal(donser[c_name].birthday,c_birth)||birthd==false))
                        cout<<c_name<<endl;
                    else
                        cout<<"NULL"<<endl;
                }
                else
                {
                    map<string,dog>::iterator it;
                    for(it=donser.begin();it!=donser.end();it++)
                    {
                        if((it->second.sex==c_sex||sexd==false)&&(deal(it->second.birthday,c_birth)||birthd==false))
                        {
                            cout<<it->first<<endl;
                            lable=true;
                        }
                    }
                    if(lable==false)
                        cout<<"NULL"<<endl;
                }
            }
            donser.clear();
        }
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/dzzy/p/8651785.html