Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
129
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. for i in 0..@x.size
  37. @@c[i][0]=0
  38. @@b[i][0]='0'
  39. i=i+1
  40. end
  41.  
  42. for j in 0..@y.size
  43. @@c[0][j]=0
  44. @@b[0][j]='0'
  45. j=j+1
  46. end
  47.  
  48. for i in 1..@x.size
  49.  
  50. for j in 1..@y.size
  51. if @x[i-1]==@y[j-1]
  52. @@c[i][j]=@@c[i-1][j-1]+1
  53. @@b[i][j] ='u'
  54. else
  55. if @@c[i-1][j] >= @@c[i][j-1]
  56. @@c[i][j] = @@c[i-1][j]
  57. @@b[i][j] ='g'
  58. else
  59.  
  60. @@c[i][j]=@@c[i][j-1]
  61. @@b[i][j]='b'
  62.  
  63. end
  64. end
  65. j=j+1
  66. end
  67. i=i+1
  68. end
  69.  
  70. q=0
  71. w=0
  72. print "\n\n\n"
  73.  
  74. for q in 0..@x.size
  75. for w in 0..@y.size
  76. print "#{@@b[q][w]}"
  77. w=w+1
  78. end
  79. print "\n"
  80. q=q+1
  81. end
  82. print "\n\n\n"
  83.  
  84. for q in 0..@x.size
  85. for w in 0..@y.size
  86. print "#{@@c[q][w]}"
  87. w=w+1
  88. end
  89. print "\n"
  90. q=q+1
  91. end
  92. print "\n\n\n"
  93.  
  94. print "\n i = #{i} , j = #{j} \n"
  95. print "NWP: "
  96. PRINTLCS(i-1,j-1,@x,@y)
  97.  
  98. end
  99.  
  100. end
  101. =begin
  102. print "Podaj 1 ciąg: "
  103. x =gets.chomp.to_s
  104. puts"1-szy ciąg bedzie brany pionowo\n\n"
  105. print "Podaj 2 ciąg: "
  106. y = gets.chomp.to_s
  107. puts "2-gi ciąg będzie brany pioziomo\n"
  108. #x=[1,0,1,1]
  109. #y =[0,0,1,0,1,0,1,1,1]
  110.  
  111. objekt = LCS.new(x,y)
  112.  
  113.  
  114.  
  115. print "\n\n\n NWP: "
  116. objekt.LCSLength(x,y)
  117.  
  118. print "\n\n\n"
  119. =end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement