fork download
  1. use warnings;
  2. my @iparray=( "ab", "bd","d");
  3. my @sortedarray=sort(@iparray);
  4. my @oparray;
  5. my $lowerlimit=0;
  6. my $upperlimit=0;
  7. my $count=0;
  8. my $inc=1;
  9. my $mat=0;
  10. my $matchedstring;
  11. my @regexarray;
  12. sub shrink
  13. {
  14. my $low = 0;
  15. my $up = 1;
  16. my $len = scalar @sortedarray;
  17. my $inc = 1;
  18. for (;$inc< $len;$inc++)
  19. {
  20. if ($sortedarray[$low] eq $sortedarray[$up])
  21. {
  22. splice(@sortedarray, $up , 1);
  23. }
  24. else
  25. {
  26. $up++;
  27. $low++;
  28. }
  29. }
  30. }
  31. sub regexp
  32. {
  33. my $inc = 0;
  34. my $regex;
  35. if(length($matchedstring ne ""))
  36. {
  37. $regex = $matchedstring . '[';
  38. }
  39. else
  40. {
  41. $regex = '[';
  42. }
  43. for (;$inc < scalar(@oparray); $inc++)
  44. {
  45. $regex = $regex . $oparray[$inc];
  46. if (($inc+1)<scalar(@oparray))
  47. {
  48. $regex = $regex.'|';
  49. }
  50. }
  51. $regex = $regex . ']';
  52. #print $regex;
  53. push @regexarray , $regex;
  54. @oparray = ();
  55. }
  56. sub splitter
  57. {
  58. #If the match is zero then increase the lowerlimit by one and call the func subroutine to identify other matches
  59.  
  60. #Else get the matched and unmatched substring from lowerlimit to upperlimit and store it into an array
  61.  
  62. #Get the matched substring
  63. $matchedstring=substr($sortedarray[$count-1],0,$mat);
  64. print("\n",$matchedstring);
  65. my $temp=$upperlimit;
  66. print("\n1.",$temp);
  67. #Traverse from lowerlimit to upperlimit to get the unmatched substring and store it into an array
  68. while($temp>=$lowerlimit)
  69. {
  70. $word=substr($sortedarray[$temp],$mat,length($sortedarray[$temp]));
  71. if($word eq "")
  72. {
  73. push @oparray,"\\s";
  74. }
  75. else
  76. {
  77. push @oparray ,$word;
  78. }
  79. $temp--;
  80.  
  81. }
  82. regexp();
  83. #print ("\n2.",@oparray);
  84. #print("\n3.",$upperlimit);
  85. #If the upperlimit reached the size of the array then endthe application
  86. if(($upperlimit)==(scalar(@sortedarray)-1))
  87. {
  88. print @regexarray;
  89. die "Terminated ";
  90. #print "All the words are parsed";
  91. #last;
  92. }
  93. #Else set the lowerlimit as upperlimit+1
  94. else
  95. {
  96. $lowerlimit=$upperlimit+1;
  97. # print ("4.",$lowerlimit);
  98. $count=$lowerlimit;
  99. $inc=1;
  100. func();
  101. }
  102.  
  103. }
  104. #print "All the words are parsed";
  105. sub func
  106. {
  107. #Get the specified number of characters from the word if it is defined in the array
  108. if (defined $sortedarray[$count])
  109. {
  110. $base=substr($sortedarray[$count],0,$inc);
  111. #print ("\n5.",$base);
  112. }
  113. #Else terminate the application
  114. else
  115. {
  116. print @regexarray;
  117. die;
  118. }
  119. #Get the specified number of characters from the word if it is defined in the array
  120. if (defined $sortedarray[$count+1])
  121. {
  122. $match=substr($sortedarray[$count+1],0,$inc) ;
  123. #print ("\n6.",$match);
  124. }
  125. #If the second word is not matched then find whether there is a match between the previous two words.
  126.  
  127. else
  128. {
  129. if (defined $sortedarray[$count-1])
  130. {
  131.  
  132.  
  133. $s=substr($sortedarray[$count-1],0,$inc);
  134. #print ("\n7.",$sortedarray[$count-1]);
  135. #print ("\n8.",$s);
  136. #print ("\n9.",$count);
  137. }
  138.  
  139. else
  140. {
  141. if($lowerlimit==0)
  142. {
  143. $upperlimit=$lowerlimit;
  144. $mat=$inc-1;
  145. splitter();
  146. }
  147. }
  148. if($s eq $base)
  149. {
  150. $upperlimit=$count;
  151. $count = $lowerlimit;
  152. $inc++;
  153. #print $inc;
  154. func();
  155. }
  156. else
  157. {
  158. $upperlimit=$lowerlimit;
  159. $mat=0;
  160. splitter();
  161. }
  162. }
  163. # print("\n",$base," ",$match);
  164. $count++;
  165. #print("\n10.",$count);
  166. #Determine whether the the two substrings were matched or not
  167. if($base ne $match)
  168. {
  169. #If the mismatch occurs at the lower limit itself means call the splitter to split the word based on the no of matches
  170. if(($count-1) == $lowerlimit)
  171. {
  172. #print ("\n11.",$mat);
  173. if(($mat==0))
  174. {
  175. if(($inc==1))
  176. {
  177. $upperlimit=$lowerlimit;
  178. splitter();
  179. }
  180. }
  181. my $lcount=$count;
  182. my $ucount=$count+1;
  183. #print("\n12.",$lcount,$ucount); #If the end of the array is reached , call the splitter to split the unmatched and matched characters.
  184. if($ucount>=scalar(@sortedarray))
  185. {
  186. $upperlimit=$count;
  187. $mat=$inc-1;
  188. # print("\n13.",$mat);
  189. splitter();
  190. }
  191. #If the mismatch occurs at the lowerlimit ,then parse the array upto the upperlimit and find any other words having the mathch
  192. else
  193. {
  194. my $ulimit=$upperlimit;
  195. #print ("\n13.",$ulimit);
  196. #print("\n14.",$lcount,"\n",$ucount,"\n",$ulimit);
  197. while($ulimit >= $count)
  198. {
  199. my $new=substr($sortedarray[$lcount],0,$inc);
  200. #print("\n15.",$new);
  201. my $new1=substr($sortedarray[$ucount],0,$inc);
  202. #print("\n16. ",$new1);
  203. #If the any two words have a match, then call the splitter to split the words before the matched words.
  204. if($new eq $new1)
  205. {
  206. $upperlimit=$lcount-1;
  207. #print("\n17.",$upperlimit);
  208. $mat=$inc-1;
  209. #print("\n18.",$mat);
  210. splitter();
  211. }
  212. #Traverse the array until the end of the array is reached
  213. else
  214. {
  215. $ulimit--;
  216. $lcount++;
  217. $ucount++;
  218. #print("\n19.",$lcount,"\n",$ucount,"\n",$ulimit);
  219. }
  220. }
  221. $mat=$inc-1;
  222. #print $mat;
  223. splitter();
  224. }
  225. }
  226. #Else increment the substring counter and do the same by calling func subroutine
  227. else
  228. {
  229. $inc++;
  230. #print $inc;
  231. unless(($count) == $upperlimit)
  232. {
  233. $count=$lowerlimit;
  234. func();
  235. }
  236. }
  237. }
  238. #If the substrings were matched change the upper limit and call func subroutine to find whether there is any other words available with the same match.
  239. else
  240. {
  241. $mat=$inc-1;
  242. #print("\n20.",$mat);
  243. $upperlimit=$count;
  244. func();
  245. }
  246. }
  247. shrink();
  248. func();
Runtime error #stdin #stdout 0s 4724KB
stdin
5 23 45 43 82 143 3
stdout

1.0

1.1

1.2[ab][bd][d]