uva 123 - Searching Quickly
这题是wa,现在没时间改,应该是单词搜寻那里有问题,搜th,the也能搜到
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<algorithm> 5 #include<sstream> 6 using namespace std; 7 typedef struct combine{ 8 string word; 9 int i; 10 string replaceword; 11 bool operator <= (const combine& b) const{ 12 return word<=b.word; 13 } 14 }combine; 15 bool cmp(combine a,combine b){ 16 if(a.word==b.word){ 17 return a.i<b.i; 18 } 19 else 20 return a.word<b.word; 21 } 22 vector<combine> dictionnary; 23 int main(){ 24 vector<string> igwords; 25 vector<string> sentence; 26 string l; 27 int len=0,temp=1; 28 int i_sentence=0; 29 int n; 30 cin>>n; 31 getchar(); 32 while(n--){ 33 getline(cin,l); 34 if(l!="::"&&temp){ 35 igwords.push_back(l); 36 continue; 37 } 38 else{ 39 if(temp){ 40 temp=0; 41 continue; 42 } 43 } 44 for(int i=0;i<l.size();i++) 45 if(isupper(l[i])) 46 l[i]=l[i]-'A'+'a'; 47 sentence.push_back(l); 48 istringstream iss(l); 49 vector<string>::iterator ig_it; 50 for(string s_temp;iss>>s_temp;){ 51 ig_it=find(igwords.begin(),igwords.end(),s_temp); 52 if(ig_it==igwords.end()){ 53 combine temp; 54 temp.word=s_temp; 55 temp.i=i_sentence; 56 for(int j=0;j<s_temp.size();j++){ 57 s_temp[j]-=32; 58 } 59 temp.replaceword=s_temp; 60 dictionnary.push_back(temp); 61 } 62 } 63 i_sentence++; 64 } 65 sort(dictionnary.begin(),dictionnary.end(),cmp); 66 string pre_word=""; 67 int pre_sentence=0; 68 int same_num=0; 69 for(int i=0;i<dictionnary.size();i++){ 70 int start_pos=0; 71 if((pre_sentence==dictionnary[i].i)&&pre_word==dictionnary[i].word){ 72 same_num++; 73 } 74 else 75 same_num=0; 76 string s_temp=sentence[dictionnary[i].i];//原title 77 for(int j=0;j<same_num;j++){ 78 start_pos=s_temp.find(dictionnary[i].word,start_pos); 79 if(!isalpha(start_pos-1)) 80 start_pos+=1; 81 } 82 start_pos=s_temp.find(dictionnary[i].word,start_pos); 83 s_temp.replace(start_pos,dictionnary[i].word.size(),dictionnary[i].replaceword); 84 cout<<s_temp<<endl; 85 pre_word=dictionnary[i].word; 86 pre_sentence=dictionnary[i].i; 87 } 88 system("pause"); 89 }
浙公网安备 33010602011771号