تغيير جنس سلسلة معينة

تغيير جنس السلسلة، أي تبديل جميع الكلمات الخاصة بالجنس في سلسلة الإدخال. 

أمثلة:

  Input:   she is my sister   Output:   he is my brother. There are two gender-specific words in this sentence:she and sister. After toggling gender specific words to their respective counterparts - he and brother : Gender specific words of the string are now changed. 

الخوارزمية:

  • احتفظ بخريطة التجزئة التي تربط جميع الكلمات المؤنثة بالكلمات المذكرة وجميع الكلمات المذكرة بالكلمات المؤنثة.
  • ثم نتحقق لكل كلمة في السلسلة مما إذا كانت هذه كلمة خاصة بالجنس أم لا. فإذا كان كذلك فإننا نستبدل هذه الكلمة بنظيرتها. وإلا فإننا لا نتبادل هذه الكلمة.
  • يتم تجميع جميع الكلمات في سلسلة جديدة تتم طباعتها في النهاية وهي السلسلة المطلوبة لدينا.
CPP
   // A C++ Program to change the gender of a string    #include          using     namespace     std  ;      // A Function that returns the new string with gender    // changed    string     changeGender  (  string     str  )      {         // A Dictionary to store the mapping of genders       // The user can add his words too.       unordered_multimap      <  string       string  >     dictionary     =         {         {  'batman'       'batwoman'  }     {  'batwoman'       'batman'  }         {  'boy'       'girl'  }     {  'girl'       'boy'  }         {  'boyfriend'       'girlfriend'  }     {  'girlfriend'       'boyfriend'  }         {  'father'       'mother'  }     {  'mother'       'father'  }         {  'husband'       'wife'  }     {  'wife'       'husband'  }         {  'he'       'she'  }     {  'she'       'he'  }         {  'his'       'her'  }     {  'her'       'his'  }         {  'male'       'female'  }     {  'female'       'male'  }         {  'man'       'woman'  }     {  'woman'       'man'  }         {  'Mr'       'Ms'  }     {  'Mr'       'Ms'  }         {  'sir'       'madam'  }     {  'madam'       'sir'  }         {  'son'       'daughter'  }     {  'daughter'       'son'  }         {  'uncle'       'aunt'  }     {  'aunt'       'uncle'  }         };         str     =     str     +     ' '  ;     // Append a space at the end       int     n     =     str  .  length  ();         // 'temp' string will hold the intermediate words       // and 'ans' string will be our result       string     temp     =     ''       ans     =     ''  ;         for     (  int     i  =  0  ;     i   <=  n  -1  ;     i  ++  )         {         if     (  str  [  i  ]     !=     ' '  )         temp  .  push_back  (  str  [  i  ]);         else      {         // If this is a 'male' or a 'female' word then       // swap this with its counterpart       if     (  dictionary  .  find  (  temp  )     !=     dictionary  .  end  ())         temp     =     dictionary  .  find  (  temp  )  ->  second  ;         ans     =     ans     +     temp     +     ' '  ;         temp  .  clear  ();         }         }         return  (  ans  );      }      // Driver Program to test above functions    int     main  ()      {         string     str     =     'she is going to watch movie with'      ' her boyfriend'  ;         cout      < <     changeGender  (  str  );         return     (  0  );      }      
Java
   import     java.util.HashMap  ;   import     java.util.Map  ;   public     class   ChangeGender     {      // A Function that returns the new string with gender changed      public     static     String     changeGender  (  String     str  )         {      // A Map to store the mapping of genders      // The user can add his words too.      Map   <  String       String  >     dictionary     =     new     HashMap   <>  ();      dictionary  .  put  (  'batman'       'batwoman'  );      dictionary  .  put  (  'batwoman'       'batman'  );      dictionary  .  put  (  'boy'       'girl'  );      dictionary  .  put  (  'girl'       'boy'  );      dictionary  .  put  (  'boyfriend'       'girlfriend'  );      dictionary  .  put  (  'girlfriend'       'boyfriend'  );      dictionary  .  put  (  'father'       'mother'  );      dictionary  .  put  (  'mother'       'father'  );      dictionary  .  put  (  'husband'       'wife'  );      dictionary  .  put  (  'wife'       'husband'  );      dictionary  .  put  (  'he'       'she'  );      dictionary  .  put  (  'she'       'he'  );      dictionary  .  put  (  'his'       'her'  );      dictionary  .  put  (  'her'       'his'  );      dictionary  .  put  (  'male'       'female'  );      dictionary  .  put  (  'female'       'male'  );      dictionary  .  put  (  'man'       'woman'  );      dictionary  .  put  (  'woman'       'man'  );      dictionary  .  put  (  'Mr'       'Ms'  );      dictionary  .  put  (  'Ms'       'Mr'  );      dictionary  .  put  (  'sir'       'madam'  );      dictionary  .  put  (  'madam'       'sir'  );      dictionary  .  put  (  'son'       'daughter'  );      dictionary  .  put  (  'daughter'       'son'  );      dictionary  .  put  (  'uncle'       'aunt'  );      dictionary  .  put  (  'aunt'       'uncle'  );      str     =     str     +     ' '  ;     // Append a space at the end      int     n     =     str  .  length  ();      // 'temp' string will hold the intermediate words      // and 'ans' string will be our result      String     temp     =     ''       ans     =     ''  ;      for     (  int     i     =     0  ;     i      <=     n     -     1  ;     i  ++  )     {      if     (  str  .  charAt  (  i  )     !=     ' '  )      temp     +=     str  .  charAt  (  i  );      else     {      // If this is a 'male' or a 'female' word then      // swap this with its counterpart      if     (  dictionary  .  containsKey  (  temp  ))      temp     =     dictionary  .  get  (  temp  );      ans     =     ans     +     temp     +     ' '  ;      temp     =     ''  ;      }      }      return     ans  ;      }      // Driver Program to test above functions      public     static     void     main  (  String  []     args  )     {      String     str     =     'she is going to watch movie with her boyfriend'  ;      System  .  out  .  println  (  changeGender  (  str  ));      }   }   
Python
   # A Python program to change the gender of a string   # A Function that returns the new string with gender changed   def   change_gender  (  string  ):   # A Dictionary to store the mapping of genders   # The user can add his words too.   dictionary   =   {   'batman'  :   'batwoman'     'batwoman'  :   'batman'     'boy'  :   'girl'     'girl'  :   'boy'     'boyfriend'  :   'girlfriend'     'girlfriend'  :   'boyfriend'     'father'  :   'mother'     'mother'  :   'father'     'husband'  :   'wife'     'wife'  :   'husband'     'he'  :   'she'     'she'  :   'he'     'his'  :   'her'     'her'  :   'his'     'male'  :   'female'     'female'  :   'male'     'man'  :   'woman'     'woman'  :   'man'     'Mr'  :   'Ms'     'Ms'  :   'Mr'     'sir'  :   'madam'     'madam'  :   'sir'     'son'  :   'daughter'     'daughter'  :   'son'     'uncle'  :   'aunt'     'aunt'  :   'uncle'     }   string   +=   ' '   # Append a space at the end   n   =   len  (  string  )   # 'temp' string will hold the intermediate words   # and 'ans' string will be our result   temp   =   ''   ans   =   ''   for   i   in   range  (  n  ):   if   string  [  i  ]   !=   ' '  :   temp   +=   string  [  i  ]   else  :   # If this is a 'male' or a 'female' word then   # swap this with its counterpart   if   temp   in   dictionary  :   temp   =   dictionary  [  temp  ]   ans   +=   temp   +   ' '   temp   =   ''   return   ans   # Driver Program to test above functions   if   __name__   ==   '__main__'  :   string   =   'she is going to watch movie with her boyfriend'   print  (  change_gender  (  string  ))   
C#
   using     System  ;   using     System.Collections.Generic  ;   class     Program   {      static     string     ChangeGender  (  string     str  )      {      // A Dictionary to store the mapping of genders      // The user can add his words too.      Dictionary   <  string       string  >     dictionary     =     new     Dictionary   <  string       string  >      {      {     'batman'       'batwoman'     }     {     'batwoman'       'batman'     }      {     'boy'       'girl'     }     {     'girl'       'boy'     }      {     'boyfriend'       'girlfriend'     }     {     'girlfriend'       'boyfriend'     }      {     'father'       'mother'     }     {     'mother'       'father'     }      {     'husband'       'wife'     }     {     'wife'       'husband'     }      {     'he'       'she'     }     {     'she'       'he'     }      {     'his'       'her'     }     {     'her'       'his'     }      {     'male'       'female'     }     {     'female'       'male'     }      {     'man'       'woman'     }     {     'woman'       'man'     }      {     'Mr'       'Ms'     }     {     'Ms'       'Mr'     }      {     'sir'       'madam'     }     {     'madam'       'sir'     }      {     'son'       'daughter'     }     {     'daughter'       'son'     }      {     'uncle'       'aunt'     }     {     'aunt'       'uncle'     }      };      str     +=     ' '  ;     // Append a space at the end      int     n     =     str  .  Length  ;      // 'temp' string will hold the intermediate words      // and 'ans' string will be our result      string     temp     =     ''       ans     =     ''  ;      for     (  int     i     =     0  ;     i      <=     n     -     1  ;     i  ++  )      {      if     (  str  [  i  ]     !=     ' '  )      {      temp     +=     str  [  i  ];      }      else      {      // If this is a 'male' or a 'female' word then      // swap this with its counterpart      if     (  dictionary  .  ContainsKey  (  temp  ))      {      temp     =     dictionary  [  temp  ];      }      ans     +=     temp     +     ' '  ;      temp     =     ''  ;      }      }      return     ans  ;      }      static     void     Main  (  string  []     args  )      {      string     str     =     'she is going to watch movie with her boyfriend'  ;      Console  .  WriteLine  (  ChangeGender  (  str  ));      Console  .  ReadKey  ();      }   }   
JavaScript
   // A Function that returns the new string with gender    // changed    function     changeGender  (  str  )     {      // A Dictionary to store the mapping of genders      // The user can add his words too.      const     dictionary     =     new     Map  ([      [  'batman'       'batwoman'  ]     [  'batwoman'       'batman'  ]      [  'boy'       'girl'  ]     [  'girl'       'boy'  ]      [  'boyfriend'       'girlfriend'  ]     [  'girlfriend'       'boyfriend'  ]      [  'father'       'mother'  ]     [  'mother'       'father'  ]      [  'husband'       'wife'  ]     [  'wife'       'husband'  ]      [  'he'       'she'  ]     [  'she'       'he'  ]      [  'his'       'her'  ]     [  'her'       'his'  ]      [  'male'       'female'  ]     [  'female'       'male'  ]      [  'man'       'woman'  ]     [  'woman'       'man'  ]      [  'Mr'       'Ms'  ]     [  'Ms'       'Mr'  ]      [  'sir'       'madam'  ]     [  'madam'       'sir'  ]      [  'son'       'daughter'  ]     [  'daughter'       'son'  ]      [  'uncle'       'aunt'  ]     [  'aunt'       'uncle'  ]      ]);      str     =     str     +     ' '  ;     // Append a space at the end      const     n     =     str  .  length  ;      // 'temp' string will hold the intermediate words      // and 'ans' string will be our result      let     temp     =     ''       ans     =     ''  ;      for     (  let     i     =     0  ;     i      <=     n     -     1  ;     i  ++  )     {      if     (  str  [  i  ]     !=     ' '  )      temp     +=     str  [  i  ];      else     {      // If this is a 'male' or a 'female' word then      // swap this with its counterpart      if     (  dictionary  .  has  (  temp  ))      temp     =     dictionary  .  get  (  temp  );      ans     =     ans     +     temp     +     ' '  ;      temp     =     ''  ;      }      }      return     (  ans  );   }   // Driver Program to test above function   const     str     =     'she is going to watch movie with her boyfriend'  ;   console  .  log  (  changeGender  (  str  ));   

الإخراج
he is going to watch movie with his girlfriend  

تعقيد الوقت: O(N^2) حيث N هو طول السلسلة حيث يمكن أن يستغرق عامل التشغيل "+"/ "إلحاق" للسلسلة ما يصل إلى وقت O(N) وبافتراض أن البحث في القاموس يستغرق O(1) وقتًا أسوأ. 

المساحة المساعدة: بصرف النظر عن القاموس الذي يقوم بتعيين جميع الكلمات إلى نظيره، فإننا نعلن عن مساحة O(N) للسلسلة الجديدة حيث N هو طول سلسلة الإدخال. 

نطاق التحسين:

  • يمكننا إضافة المزيد من الكلمات ونظائرها في القاموس لزيادة دقة البرنامج. على سبيل المثال يمكننا أن نضيف – الممثلة الممثلة إلهة الله إلى قاموسنا.
  • يمكن أيضًا استيراد ملف نصي يحتوي على كلمات جميع الكلمات المؤنثة والمذكرة.
  • يمكن تعديل البرنامج ليصبح غير حساس لحالة الأحرف.