1 #include <iostream> 2 #include <cstring> 3 #include <vector> 4 #include <string> 5 #include <malloc> 6 7 using namespace std; 8 9 #define NULL 0 10 #define LEN sizeof(struct wordList) 11 12 struct wordList 13 { 14 string orginal; 15 string real; 16 struct wordList *next; 17 }; 18 19 struct wordList * create(int wordListNumber) 20 { 21 struct wordList *head,*p1,*p2; 22 int i=0; 23 head=NULL; 24 p1=p2=(struct wordList*)malloc(LEN); 25 cin>>p1->orginal>>p1->real; 26 while(i<wordListNumber) 27 { 28 i++; 29 if (i==1) 30 { 31 head=p1; 32 } 33 else 34 { 35 p2->next=p1; 36 } 37 p2=p1; 38 p1=(struct wordList*)malloc(LEN); 39 cin>>p1->orginal>>p1->real; 40 } 41 p1->next=NULL; 42 return(head); 43 } 44 45 vector<string> split(string &sentence,string separator) 46 { 47 vector<string> returnStr; 48 string::size_type position1,position2; 49 position2=sentence.find(separator); 50 position1=0; 51 while(string::npos!=position2) 52 { 53 returnStr.push_back(sentence.substr(position1,position2-position1)); 54 position1=position2+1; 55 position2=sentence.find(separator); 56 } 57 returnStr.push_back(sentence.substr(position1)); 58 return returnStr; 59 } 60 61 void translate(vector<string> &singleWord,struct wordList* p) 62 { 63 int i=0; 64 for (i=0;i<singleWord.size();i++) 65 { 66 while (p->next!=NULL) 67 { 68 if (singleWord[i]==p->orginal) 69 { 70 singleWord[i]=p->real; 71 } 72 } 73 } 74 } 75 76 int main() 77 { 78 int T,N,M,i; 79 string sentence; 80 struct wordList *p; 81 cin>>T; 82 for (i=0;i<T;i++) 83 { 84 cin>>N>>M; 85 p=create(M); 86 cin.get(); 87 getline(cin,sentence,'\n'); 88 vector<string> singleWord=split(sentence," "); 89 translate(singleWord,p); 90 for (i=0;i<singleWord.size()-1;i++) 91 { 92 cout<<singleWord[i]<<" "; 93 } 94 cout<<singleWord[singleWord.size()-1]<<endl; 95 } 96 return 0; 97 }
实在找不到原因,如果你刚好看到,刚好又会,烦请指教。