Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class ADN {
  5. public class Node{// Nodes for the tree
  6. int len;
  7. char L[];
  8. int actualsize;// number of sons
  9. Node son[];
  10.  
  11. public Node(String letras){
  12. len=letras.length();
  13. L= new char[len];
  14. for(int i=0;i<letras.length();i++){
  15. L[i]=letras.charAt(i);
  16. }
  17. son= new Node[4];
  18. actualsize=0;
  19. }
  20. }
  21. Node root;
  22.  
  23. public ADN(String root) {
  24. this.root=new Node(root);
  25. }
  26.  
  27. public void addNode(String adn){
  28. Node n= new Node(adn);
  29. addNode(n,root,0);
  30. }
  31. private void addNode(Node n, Node root, int nivel){//check that
  32. if(n.L[nivel]=='A'){
  33. if(root.son[0]==null)root.son[0]=n;
  34. else addNode(n,root.son[0],nivel+1);
  35. }else if(n.L[nivel]=='D'){
  36. if(root.son[1]==null)root.son[1]=n;
  37. else addNode(n,root.son[1],nivel+1);
  38. }else if(n.L[nivel]=='G'){
  39. if(root.son[2]==null)root.son[2]=n;
  40. else addNode(n,root.son[2],nivel+1);
  41. }else if(n.L[nivel]=='T'){
  42. if(root.son[3]==null)root.son[3]=n;
  43. else addNode(n,root.son[3],nivel+1);
  44. }
  45. root.actualsize++;
  46. }
  47. public int searchNode(String letras){
  48. Node n= new Node(letras);
  49. return searchNode(n,root,0);
  50. }
  51. public boolean equalsNode(Node n1, Node n2){
  52. boolean aux = false;
  53. for(int i=0;i<n1.len;i++){
  54. if(n1.L[i]==n2.L[i]){
  55. aux=true;
  56. }else{
  57. return false;
  58. }
  59. }
  60. return aux;
  61. }
  62. public int searchNode(Node n, Node root, int nivel){
  63. if(n.L[nivel]=='A'){
  64. if(root.son[0]==null || equalsNode(root,n)){
  65. return nivel;
  66. }
  67. else {
  68. return searchNode(n,root.son[0],nivel+1);
  69. }
  70. }else if(n.L[nivel]=='D'){
  71. if(root.son[1]==null || equalsNode(root,n)){
  72. return nivel;
  73. }
  74. else {
  75. return searchNode(n,root.son[1],nivel+1);
  76. }
  77. }else if(n.L[nivel]=='G'){
  78. if(root.son[2]==null || equalsNode(root,n)){
  79. return nivel;
  80. }
  81. else {
  82. return searchNode(n,root.son[2],nivel+1);
  83. }
  84. }else if(n.L[nivel]=='T'){
  85. if(root.son[3]==null || equalsNode(root,n)){
  86. return nivel;
  87. }
  88. else {
  89. return searchNode(n,root.son[3],nivel+1);
  90. }
  91. }else{// do not have to enter.
  92. return nivel;
  93. }
  94. }
  95.  
  96. /*public int search(Node n, int len, Node root){
  97. int number=1;
  98. for(int i =0; i<len;i++){// for to check all the letters
  99. if(n.L[i]=='A'){
  100. if(root.son[0]==n){
  101. break;
  102. }
  103. else
  104. {
  105. number=search(n,len,Node root.son[0]);
  106. }
  107. }else if(n.L[i]=='D'){
  108.  
  109. }else if(n.L[i]=='G'){
  110.  
  111. }else if(n.L[i]=='T'){
  112.  
  113. }
  114. }
  115. return number;
  116. }*/
  117.  
  118. public static void main(String[] args){
  119. int C = 0; // cases number
  120. int L = 0; // Length of the chains
  121. int N = 0; // number of sequences to introduce
  122. int B = 0; // number of searches to do
  123. int results[][] = null;
  124. //ArrayList<Integer> []results =new ArrayList<Integer>()[C];
  125. ADN adn= null;
  126. String aux=null;
  127. String searches= null;
  128. Scanner s = new Scanner(System.in);
  129. C= s.nextInt();
  130. results= new int[C][100];
  131. for(int i=0;i<C;i++){
  132. for(int j=0;j<100;j++){
  133. results[i][j]=-1;
  134. }
  135. }
  136. for(int c=0;c<C;c++){//Cycle for the number of cases
  137. L=0;
  138. N=0;
  139. adn=null;
  140. aux= null;
  141. B=0;
  142. searches=null;
  143. L= s.nextInt();//maybe we will not use it
  144. N= s.nextInt();
  145. adn = new ADN("");//watch out for the root!
  146. for(int n= 0;n<N;n++){
  147. aux=s.next();
  148. adn.addNode(aux);
  149. }
  150. B= s.nextInt();
  151. //results= new int[B];
  152. /*for(int i=0;i<C;i++){
  153. results[c][i]= 0;
  154. }*/
  155.  
  156.  
  157. for(int b= 0; b<B;b++){
  158. searches=s.next();
  159. results[c][b]= adn.searchNode(searches);
  160. }
  161.  
  162. }
  163.  
  164. for(int c=0;c<C;c++){
  165. for(int b= 0;b<100;b++){
  166. if(results[c][b]!=-1)
  167. System.out.print(results[c][b]+" ");
  168.  
  169. }
  170. System.out.println();
  171. }
  172.  
  173.  
  174.  
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement