程式設計珠璣課後題答案-3.5
阿新 • • 發佈:2019-01-01
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <cassert> using namespace std; string table[23]={"et-ic","al-is-tic","s-tic","p-tic","-lyt-ic","ot-ic", "an-tic","n-tic","c-tic""at-ic","h-nic","n-ic","m-ic","l=lic","b-lic", "-clic","l-ic","h-ic","f-ic","d-ic","-bic","a-ic","-mac","i-ac"}; bool compare(string a,string b) { string::iterator ia=a.end()-1; string::iterator ib=b.end()-1; while(ia!=a.begin() && ib!=b.begin()) { if(*--ia != *--ib) return (*ia)<(*ib); } return (*ia)<(*ib); } int main() { vector<string> Table; for(int i=0;i<23;i++) { Table.push_back(table[i]); } string Input("eth-nic"); //cout<<"輸入單詞:"; //cin>>Input; //sort(Table.begin(),Table.end(),compare); for(vector<string>::iterator i=Table.begin();i!=Table.end();i++) { int m=(*i).size()-2; int n=Input.size()-2; while(m>=0 && n>=0) { if((*i)[m]!=Input[n]) break; m--; n--; } if(m==-1) cout<<*i<<endl; } system("pause"); return 0; }