uopspop

Untitled

Apr 8th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. char flipChar(char c){
  5.     if (c == '+') return '-';
  6.     if (c == '-') return '+';
  7. }
  8.  
  9.  
  10. int main() {
  11.     // your code goes here
  12.     int T;
  13.     cin >> T;
  14.     int caseIndex = 1;
  15.     while(T){
  16.         T--;
  17.         cout << "T: " << T << "\n";
  18.  
  19.         string str;
  20.         cin >> str;
  21. //      cout << "str: " << str << "\n";
  22.         int K;
  23.         cin >> K;
  24. //      cout << "K: " << K << "\n";
  25.  
  26.  
  27.         // start from left the right, it the remain length is less thatn K, end it
  28.         int start_index = 0;
  29.         int end_index = str.length()-1;
  30.  
  31. //      cout << "start_index: " << start_index << "\n";
  32. //      cout << "end_index: " << end_index << "\n";
  33.  
  34.         int result_count = 0;
  35.  
  36. //        cout << "before flipping: " << str << "\n";
  37.         while( (end_index - start_index + 1) >= K ){
  38.             // 若能進來,代表還可以至少翻一次
  39. //            cout << "start_index: " << start_index << "\n";
  40.  
  41.             // 從左邊開始,找出最邊邊的'-'
  42.             while(str[start_index] != '-' && (end_index - start_index + 1) >= K){
  43.                 start_index++;
  44.             }
  45.             // 若已經找到不能再翻的index,就離開
  46.             if ( (end_index - start_index + 1) < K ){
  47.                 break;
  48.             }
  49.             // 現在已經找到最邊邊的'-',開始翻(這邊需要再優化)
  50.             for (int i = start_index; i < start_index + K; i++){
  51.                 str[i] = flipChar(str[i]);
  52.             }
  53.             result_count++;
  54. //            cout << "after  flipping: " << str << "\n";
  55.  
  56.         }// end of inner while
  57.  
  58.         std::size_t found = str.find('-');
  59.         if (found==std::string::npos){
  60.             cout << "Case #" << caseIndex << ": " << result_count << "\n";
  61.         }else{
  62.             cout << "Case #" << caseIndex << ": " << "IMPOSSIBLE" << "\n";
  63.         }
  64.  
  65.         caseIndex++;
  66.  
  67.     }
  68.  
  69.  
  70.     return 0;
  71. }
Add Comment
Please, Sign In to add comment