miro_ivanov

TitleSearch

Jun 9th, 2023
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace TitleSearch
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string word = Console.ReadLine();
  13.  
  14. int count = int.Parse(Console.ReadLine());
  15. string[] phrases = new string[count];
  16.  
  17. for (int i = 0; i < count; i++)
  18. {
  19. phrases[i] = Console.ReadLine();
  20. }
  21.  
  22. for (int i = 0; i < phrases.Length; i++)
  23. {
  24. if (exist(phrases[i], word))
  25. {
  26. modify(ref word, phrases[i]);
  27. Console.WriteLine(word);
  28. }
  29. else Console.WriteLine("No such title found!");
  30. }
  31.  
  32. }
  33.  
  34. public static bool exist(string phrase, string word)
  35. {
  36. char[] phraseChar = phrase.ToCharArray();
  37. for (int i = 0; i < phraseChar.Length; i++)
  38. {
  39. if (!word.Contains(phraseChar[i]))
  40. {
  41. return false;
  42. }
  43. }
  44. return true;
  45. }
  46.  
  47. public static string modify(ref string word, string phrase)
  48. {
  49. char[] wordChar = word.ToCharArray();
  50. char[] phraseChar = phrase.ToCharArray();
  51. StringBuilder sb = new StringBuilder();
  52. List<int> indexes = new List<int>();
  53. for (int i = 0; i < phraseChar.Length; i++)
  54. {
  55. int index = word.IndexOf(phraseChar[i]);
  56. if (indexes.Contains(index))
  57. {
  58. index = word.IndexOf(phraseChar[i], word.IndexOf(phraseChar[i]) + 1);
  59. }
  60. wordChar[index] = ' ';
  61. indexes.Add(index);
  62. }
  63. for (int i = 0; i < wordChar.Length; i++)
  64. {
  65. if (wordChar[i] != ' ')
  66. {
  67. sb.Append(wordChar[i]);
  68. }
  69. }
  70. word = sb.ToString();
  71. return word;
  72.  
  73. }
  74. }
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment