document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include<cstring>
  5. #include<algorithm>
  6.  
  7. using namespace::std;
  8. //包括遇到HTML entity時候要包起來
  9. int main()
  10. {
  11.    
  12.     string s="123&TWD;432&JPY;";
  13.     string temp="";
  14.    
  15.     vector<string> buffer;
  16.  
  17.    
  18.     int head =0;
  19.     int peak =0;
  20.     int tail=s.size()-1;
  21.    
  22.     cout<<s<<endl;
  23.    
  24.     while(head<tail){
  25.         if(s[head]==\'&\'){
  26.             peak=head;
  27.             ++peak;  //不存&
  28.             do{
  29.                 temp+=s[peak];
  30.                 ++peak;
  31.             }while(s[peak]!=\';\');
  32.             temp+=s[peak];
  33.             buffer.push_back(temp);
  34.             s.replace(head+1,peak-head,"");
  35.             tail-=peak-head;
  36.             temp="";
  37.         }
  38.        
  39.         if(s[tail]==\';\'){
  40.             peak=tail;
  41.             do{
  42.                 temp+=s[peak];
  43.                 --peak;
  44.             }while(s[peak]!=\'&\');
  45.             //不存& 所以沒有再一次temp+=s[peak];
  46.             reverse(temp.begin(),temp.end());
  47.             buffer.push_back(temp);
  48.             s.replace(peak+1,tail-peak,"");
  49.             tail=peak;
  50.             temp="";
  51.         }
  52.        
  53.         swap(s[head],s[tail]);
  54.         head++;
  55.         tail--;
  56.     }
  57.  
  58.     int last = buffer.size()-1;
  59.  
  60.     for(int i=0;i<s.length();i++){
  61.         if(s[i]==\'&\'){
  62.             s.insert(i+1,buffer[last]);
  63.             last--;
  64.         }
  65.        
  66.     }
  67.        
  68.     cout<<s<<endl;
  69.    
  70.    
  71.     return 0;
  72.  
  73. }
');