以下是我的代碼,但是在通過uva測試是time limit exceeded, 不知道是什麼原因,而且我自己測試了幾個數據都是對的。
[cpp]
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<stdio.h>
#include<algorithm>
using namespace std;
string Erdos="Erdos, P.";
string Scenario="Scenario ";
string Infinity="infinity";
map<string,int> Scientists;
void loopFind(map<int,vector<string> > map_name,vector<string> parters,vector<int> lines,int value){
if(parters.size()<=0)return;
for(int i=0;i<parters.size();++i){
vector<string> parter1;
vector<int> lines1;
string tmp=parters.at(i);
for(map<int,vector<string> >::iterator it=map_name.begin();it!=map_name.end();++it){
vector<string> tmpNames=it->second;
vector<int>::iterator tmpIndex=find(lines.begin(),lines.end(),it->first);
vector<string>::iterator it1=find(tmpNames.begin(),tmpNames.end(),tmp);
if(tmpIndex!=lines.end())break;
else if(it1!=tmpNames.end()){
for(int j=0;j<tmpNames.size();++j){
string tmp1=tmpNames.at(j);
if(tmp1!=tmp){
Scientists[tmp1]=value;
parter1.push_back(tmp1);
}
}
lines1.push_back(it->first);
}
}
loopFind(map_name,parter1,lines1,value+1);
}
}
void findScientists(map<int,vector<string> > map_name){
Scientists[Erdos]=0;
vector<string> parters;
vector<int> lines;
for(map<int,vector<string> >::iterator it=map_name.begin();it!=map_name.end();++it){
vector<string> names=it->second;
vector<string>::iterator tmp=find(names.begin(),names.end(),Erdos);
if(tmp!=names.end()){
for(int i=0;i<names.size();++i){
string tmp1=names.at(i);
if(tmp1!=Erdos){
Scientists[tmp1]=1;
parters.push_back(tmp1);
}
}
lines.push_back(it->first);
}
}
loopFind(map_name,parters,lines,2);
}
void getErdosNumber(vector<string> paper,vector<string> names){
int index=1;
map<int,vector<string> > map_name;
for(vector<string>:: iterator it=paper.begin();it!=paper.end();++it){
string tmpString=*it;
vector<string> tmpSci;
int begin=0;
int end=0;
if(tmpString.length()<1)continue;
while(end<tmpString.length()){
if(tmpString[end]=='.'&&tmpString[end+1]==','&&tmpString[end+2]==' '){
string tmpName=tmpString.substr(begin,end-begin+1);
tmpSci.push_back(tmpName);
begin=end+3;
end=begin;
}
else if(tmpString[end]==':'){
string tmpName=tmpString.substr(begin,end-begin);
tmpSci.push_back(tmpName);
break;
}
else ++end;
}
map_name[index]=tmpSci;
++index;
}
findScientists(map_name);
}
int main(){
int n=0;
cin>>n;
for(int i=1;i<=n;++i){
int P=0;
int N=0;
cin>>P>>N;
if(P<1||N<1)continue;
getchar();
vector<string> paper;
for(int x=0;x<P;++x){
string tmpPaper;
getline(cin,tmpPaper);
paper.push_back(tmpPaper);
}
vector<string> names;
for(int y=0;y<N;++y){
string tmpName;
getline(cin,tmpName);
names.push_back(tmpName);
}
Scientists.clear();
getErdosNumber(paper,names);
cout<<Scenario<<i<<endl;
map<string,int>::iterator it;
for(vector<string>::iterator ite=names.begin();ite!=names.end();++ite){
it=Scientists.find(*ite);
if(it!=Scientists.end()){
cout<<it->first<<" "<<it->second<<endl;
}
else{
cout<<*ite<<" "<<Infinity<<endl;
}
}
}
return 0;
}
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<stdio.h>
#include<algorithm>
using namespace std;
string Erdos="Erdos, P.";
string Scenario="Scenario ";
string Infinity="infinity";
map<string,int> Scientists;
void loopFind(map<int,vector<string> > map_name,vector<string> parters,vector<int> lines,int value){
if(parters.size()<=0)return;
for(int i=0;i<parters.size();++i){
vector<string> parter1;
vector<int> lines1;
string tmp=parters.at(i);
for(map<int,vector<string> >::iterator it=map_name.begin();it!=map_name.end();++it){
vector<string> tmpNames=it->second;
vector<int>::iterator tmpIndex=find(lines.begin(),lines.end(),it->first);
vector<string>::iterator it1=find(tmpNames.begin(),tmpNames.end(),tmp);
if(tmpIndex!=lines.end())break;
else if(it1!=tmpNames.end()){
for(int j=0;j<tmpNames.size();++j){
string tmp1=tmpNames.at(j);
if(tmp1!=tmp){
Scientists[tmp1]=value;
parter1.push_back(tmp1);
}
}
lines1.push_back(it->first);
}
}
loopFind(map_name,parter1,lines1,value+1);
}
}
void findScientists(map<int,vector<string> > map_name){
Scientists[Erdos]=0;
vector<string> parters;
vector<int> lines;
for(map<int,vector<string> >::iterator it=map_name.begin();it!=map_name.end();++it){
vector<string> names=it->second;
vector<string>::iterator tmp=find(names.begin(),names.end(),Erdos);
if(tmp!=names.end()){
for(int i=0;i<names.size();++i){
string tmp1=names.at(i);
if(tmp1!=Erdos){
Scientists[tmp1]=1;
parters.push_back(tmp1);
}
}
lines.push_back(it->first);
}
}
loopFind(map_name,parters,lines,2);
}
void getErdosNumber(vector<string> paper,vector<string> names){
int index=1;
map<int,vector<string> > map_name;
for(vector<string>:: iterator it=paper.begin();it!=paper.end();++it){
string tmpString=*it;
vector<string> tmpSci;
int begin=0;
int end=0;
if(tmpString.length()<1)continue;
while(end<tmpString.length()){
if(tmpString[end]=='.'&&tmpString[end+1]==','&&tmpString[end+2]==' '){
string tmpName=tmpString.substr(begin,end-begin+1);
tmpSci.push_back(tmpName);
begin=end+3;
end=begin;
}
else if(tmpString[end]==':'){
string tmpName=tmpString.substr(begin,end-begin);
tmpSci.push_back(tmpName);
break;
}
else ++end;
}
map_name[index]=tmpSci;
++index;
}
findScientists(map_name);
}
int main(){
int n=0;
cin>>n;
for(int i=1;i<=n;++i){
int P=0;
int N=0;
cin>>P>>N;
if(P<1||N<1)continue;
getchar();
vector<string> paper;
for(int x=0;x<P;++x){
string tmpPaper;
getline(cin,tmpPaper);
paper.push_back(tmpPaper);
}
vector<string> names;
for(int y=0;y<N;++y){
string tmpName;
getline(cin,tmpName);
names.push_back(tmpName);
}
Scientists.clear();
getErdosNumber(paper,names);
cout<<Scenario<<i<<endl;
map<string,int>::iterator it;
for(vector<string>::iterator ite=names.begin();ite!=names.end();++ite){
it=Scientists.find(*ite);
if(it!=Scientists.end()){
cout<<it->first<<" "<<it->second<<endl;
}
else{
cout<<*ite<<" "<<Infinity<<endl;
}
}
}
return 0;
}
如果有人知道為什麼會是time limit exceeded, 煩請告知,謝謝