题目地址http://pat.zju.edu.cn/contests/pat-a-practise/1039
思路分析:题目简单,唯一难点在于测试点5,容易超时
注意:不要使用iostream,会超时
C++版本(测试点5不通过)
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
vector<int> stu[26][26][26][10];
int main()
{
//freopen("C:\\Users\\Devon\\Desktop\\input.txt","r",stdin);
for(int i=0;i<26;i++)
for(int j=0;j<26;j++)
for(int k=0;k<26;k++)
for(int x=0;x<10;x++)
stu[i][j][k][x].clear();
int n,k;
cin>>n>>k;
for(int i=0;i<k;i++){
int cno,sno;
cin>>cno>>sno;
for(int j=0;j<sno;j++){
string sname;
cin>>sname;
stu[sname.at(0)-'A'][sname.at(1)-'A'][sname.at(2)-'A'][sname.at(3)-'0'].push_back(cno);
}
}
for(int i=0;i<n;i++){
string sname;
cin>>sname;
vector<int> temp=stu[sname.at(0)-'A'][sname.at(1)-'A'][sname.at(2)-'A'][sname.at(3)-'0'];
cout<<sname<<" "<<temp.size();
sort(temp.begin(),temp.end());
for(int j=0;j<temp.size();j++)
cout<<" "<<temp[j];
cout<<endl;
}
return 0;
}
C++的其他解决方案(二分查找)(测试点5没通过。。)
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
struct record{
string sname;
int cno;
bool operator <(const record &A) const{
if(A.sname==sname) return cno<A.cno;
else return sname.compare(A.sname)<0;
}
};
vector<record> s;
int getlow(string sname){
int low=0;
int high=s.size()-1;
int middle=(low+high)/2;
while(low<=high){
if(sname.compare(s[middle].sname)==0) high=middle-1;
else if(sname.compare(s[middle].sname)>0) low=middle+1;
else high=middle-1;
middle=(low+high)/2;
}
return middle;
}
int gethigh(string sname){
int low=0;
int high=s.size()-1;
int middle=(low+high)/2;
while(low<=high){
if(sname.compare(s[middle].sname)==0) low=middle+1;
else if(sname.compare(s[middle].sname)>0) low=middle+1;
else high=middle-1;
middle=(low+high)/2;
}
return middle;
}
int main()
{
//freopen("C:\\Users\\Devon\\Desktop\\input.txt","r",stdin);
s.clear();
int n,k;
cin>>n>>k;
for(int i=0;i<k;i++){
int cno,sno;
cin>>cno>>sno;
for(int j=0;j<sno;j++){
record temp;
cin>>temp.sname;
temp.cno=cno;
s.push_back(temp);
}
}
sort(s.begin(),s.end());
for(int i=0;i<n;i++){
string sname;
cin>>sname;
int low=getlow(sname);
if(sname.compare(s[low].sname)!=0) low++;
int high=gethigh(sname);
if(sname.compare(s[low].sname)!=0) high--;
if(sname.compare(s[low].sname)!=0) cout<<sname<<" "<<0;
else{
cout<<sname<<" "<<high-low+1;
for(int j=low;j<=high;j++)
cout<<" "<<s[j].cno;
}
cout<<endl;
}
return 0;
}
C++版本(测试点全部通过)
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
vector<int> stu[26][26][26][10];
int main()
{
//freopen("C:\\Users\\Devon\\Desktop\\input.txt","r",stdin);
for(int i=0;i<26;i++)
for(int j=0;j<26;j++)
for(int k=0;k<26;k++)
for(int x=0;x<10;x++)
stu[i][j][k][x].clear();
int n,k;
scanf("%d%d",&n,&k);
for(int i=0;i<k;i++){
int cno,sno;
scanf("%d%d",&cno,&sno);
for(int j=0;j<sno;j++){
char sname[5];
scanf("%s",sname);
stu[sname[0]-'A'][sname[1]-'A'][sname[2]-'A'][sname[3]-'0'].push_back(cno);
}
}
for(int i=0;i<n;i++){
char sname[5];
scanf("%s",sname);
vector<int> temp=stu[sname[0]-'A'][sname[1]-'A'][sname[2]-'A'][sname[3]-'0'];
printf("%s %d",sname,temp.size());
sort(temp.begin(),temp.end());
for(int j=0;j<temp.size();j++)
printf(" %d",temp[j]);
printf("\n");
}
return 0;
}