fork(4) download
  1. using System;
  2.  
  3. namespace MyNameSpace
  4. {
  5. class Stack
  6. {
  7. private Fragment[] _stack;
  8. private int top = -1;
  9. private int length;
  10. public Stack(int length)
  11. {
  12. this._stack = new Fragment[length];
  13. this.length = length;
  14. }
  15.  
  16. public void Push(Fragment value)
  17. {
  18. if (top == length - 1)
  19. {
  20. throw new InvalidOperationException("stack is full");
  21. }
  22.  
  23. top++;
  24. this._stack[top] = value;
  25. }
  26.  
  27. public Fragment Pop()
  28. {
  29. if (top == -1)
  30. {
  31. throw new InvalidOperationException("Stack is empty");
  32. }
  33.  
  34. var value = this._stack[top];
  35. top--;
  36. return value;
  37. }
  38.  
  39. public Fragment Top()
  40. {
  41. if (top == -1)
  42. {
  43. throw new InvalidOperationException("Stack is empty");
  44. }
  45.  
  46. return this._stack[top];
  47. }
  48.  
  49. public void PrintStack()
  50. {
  51. for (int i = 0; i <= top; i++)
  52. {
  53. Console.Write("{0} ", this._stack[i]);
  54. }
  55. }
  56. public bool IsEmpty()
  57. {
  58. return top == -1;
  59. }
  60. }
  61.  
  62. class Fragment
  63. {
  64. public string value;
  65. public int level;
  66. public bool hasImageFile = false;
  67. private string _path;
  68.  
  69. public Fragment(string value, int level)
  70. {
  71. this.level = level;
  72. this.value = value.Trim();
  73. }
  74.  
  75. public string getAbsolutePath()
  76. {
  77. return this._path;
  78. }
  79.  
  80. public void setAbsolutePath(string parentPath)
  81. {
  82. this._path = string.Concat(parentPath, "/", this.value);
  83. }
  84.  
  85. public bool isDirectory()
  86. {
  87. return !this.value.Contains(".");
  88. }
  89.  
  90. public bool isImageFile()
  91. {
  92. foreach (var ext in imageFileExtensions)
  93. {
  94. if (this.value.EndsWith(ext))
  95. {
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101.  
  102. private static string[] imageFileExtensions = { ".jpeg", ".png", ".gif" };
  103.  
  104. }
  105.  
  106. class Program
  107. {
  108. static void Main(string[] args)
  109. {
  110. string str = "dir1\n dir11\n dir12\n picture.jpeg\n dir 121\n file1.txt\ndir2\n file2.gif";
  111. Console.WriteLine(getLengthOfPathContainintImageFiles(str));
  112. Console.Read();
  113. }
  114.  
  115. private static int getLengthOfPathContainintImageFiles(string str)
  116. {
  117. int length = 0;
  118. string[] paths = getListOfPaths(str);
  119. Stack stack = new Stack(paths.Length);
  120.  
  121. int i = 0;
  122. while(i < paths.Length)
  123. {
  124. Fragment fragment = new Fragment(paths[i], getLevel(paths[i]));
  125. if (!stack.IsEmpty() && stack.Top().level >= fragment.level)
  126. {
  127. stack.Pop();
  128. continue;
  129. }
  130.  
  131. if (stack.IsEmpty())
  132. {
  133. fragment.setAbsolutePath("");
  134. stack.Push(fragment);
  135. i++;
  136. continue;
  137. }
  138.  
  139. if (fragment.isDirectory()) {
  140. var parentPath = stack.IsEmpty() ? "" : stack.Top().getAbsolutePath();
  141. fragment.setAbsolutePath(parentPath);
  142. stack.Push(fragment);
  143. i++;
  144. continue;
  145. }
  146.  
  147. if (stack.Top().hasImageFile == false && fragment.isImageFile()) {
  148. length += stack.Top().getAbsolutePath().Length;
  149. stack.Top().hasImageFile = true;
  150. }
  151.  
  152. i++;
  153. }
  154.  
  155. return length;
  156. }
  157.  
  158. private static int getLevel(string str)
  159. {
  160. int indentationCount = 0;
  161. for (int i = 0; i < str.Length; i++)
  162. {
  163. if (str[i] == ' ')
  164. {
  165. indentationCount++;
  166. }
  167. else
  168. {
  169. break;
  170. }
  171. }
  172. return indentationCount;
  173. }
  174.  
  175. private static string[] getListOfPaths(string str)
  176. {
  177. return str.Split('\n');
  178. }
  179.  
  180. }
  181. }
  182.  
Success #stdin #stdout 0.06s 24168KB
stdin
Standard input is empty
stdout
16