Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. class LCS
  2.  
  3.  
  4. @@c=Array.new(100){Array.new(100,0)}
  5. @@b=Array.new(100){Array.new(100,0)}
  6. @@zlicz=0
  7. attr_accessor :x , :y
  8.  
  9. def initialize(a,b)
  10. @x=a
  11. @y =b
  12. end
  13.  
  14.  
  15. def PRINTLCS(i,j,x,y)
  16. if i==0 || j==0
  17. return
  18. end
  19.  
  20. if @@b[i][j] == 'u'
  21. PRINTLCS(i-1,j-1,@x,@y)
  22. print "#{@x[i-1]}"
  23.  
  24. elsif @@b[i][j] == 'g'
  25. PRINTLCS(i-1,j,@x,@y)
  26. elsif @@b[i][j] == 'b'
  27. PRINTLCS(i,j-1,@x,@y)
  28. end
  29. end
  30.  
  31.  
  32. def LCSLength(x,y)
  33. i=0
  34. j=0
  35.  
  36. @@c[i][0]=0
  37. @@b[i][0]='0'
  38. i=i+1
  39. end
  40.  
  41. @@c[0][j]=0
  42. @@b[0][j]='0'
  43. j=j+1
  44. end
  45.  
  46.  
  47. if @x[i-1]==@y[j-1]
  48. @@c[i][j]=@@c[i-1][j-1]+1
  49. @@b[i][j] ='u'
  50. else
  51. if @@c[i-1][j] >= @@c[i][j-1]
  52. @@c[i][j] = @@c[i-1][j]
  53. @@b[i][j] ='g'
  54. else
  55.  
  56. @@c[i][j]=@@c[i][j-1]
  57. @@b[i][j]='b'
  58.  
  59. end
  60. end
  61. j=j+1
  62. end
  63. i=i+1
  64. end
  65.  
  66. q=0
  67. w=0
  68. print "\n\n\n"
  69.  
  70. print "#{@@b[q][w]}"
  71. w=w+1
  72. end
  73. print "\n"
  74. q=q+1
  75. end
  76. print "\n\n\n"
  77.  
  78. print "#{@@c[q][w]}"
  79. w=w+1
  80. end
  81. print "\n"
  82. q=q+1
  83. end
  84. print "\n\n\n"
  85.  
  86. print "\n i = #{i} , j = #{j} \n"
  87. print "NWP: "
  88. PRINTLCS(i-1,j-1,@x,@y)
  89.  
  90. end
  91.  
  92. end
  93. =begin
  94. print "Podaj 1 ciąg: "
  95. x =gets.chomp.to_s
  96. puts"1-szy ciąg bedzie brany pionowo\n\n"
  97. print "Podaj 2 ciąg: "
  98. y = gets.chomp.to_s
  99. puts "2-gi ciąg będzie brany pioziomo\n"
  100. #x=[1,0,1,1]
  101. #y =[0,0,1,0,1,0,1,1,1]
  102.  
  103. objekt = LCS.new(x,y)
  104.  
  105.  
  106.  
  107. print "\n\n\n NWP: "
  108. objekt.LCSLength(x,y)
  109.  
  110. print "\n\n\n"
  111. =end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement