Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class ADN {
- public class Node{// Nodes for the tree
- int len;
- char L[];
- int actualsize;// number of sons
- Node son[];
- public Node(String letras){
- len=letras.length();
- L= new char[len];
- for(int i=0;i<letras.length();i++){
- L[i]=letras.charAt(i);
- }
- son= new Node[4];
- actualsize=0;
- }
- }
- Node root;
- public ADN(String root) {
- this.root=new Node(root);
- }
- public void addNode(String adn){
- Node n= new Node(adn);
- addNode(n,root,0);
- }
- private void addNode(Node n, Node root, int nivel){//check that
- if(n.L[nivel]=='A'){
- if(root.son[0]==null)root.son[0]=n;
- else addNode(n,root.son[0],nivel+1);
- }else if(n.L[nivel]=='D'){
- if(root.son[1]==null)root.son[1]=n;
- else addNode(n,root.son[1],nivel+1);
- }else if(n.L[nivel]=='G'){
- if(root.son[2]==null)root.son[2]=n;
- else addNode(n,root.son[2],nivel+1);
- }else if(n.L[nivel]=='T'){
- if(root.son[3]==null)root.son[3]=n;
- else addNode(n,root.son[3],nivel+1);
- }
- root.actualsize++;
- }
- public int searchNode(String letras){
- Node n= new Node(letras);
- return searchNode(n,root,0);
- }
- public boolean equalsNode(Node n1, Node n2){
- boolean aux = false;
- for(int i=0;i<n1.len;i++){
- if(n1.L[i]==n2.L[i]){
- aux=true;
- }else{
- return false;
- }
- }
- return aux;
- }
- public int searchNode(Node n, Node root, int nivel){
- if(n.L[nivel]=='A'){
- if(root.son[0]==null || equalsNode(root,n)){
- return nivel;
- }
- else {
- return searchNode(n,root.son[0],nivel+1);
- }
- }else if(n.L[nivel]=='D'){
- if(root.son[1]==null || equalsNode(root,n)){
- return nivel;
- }
- else {
- return searchNode(n,root.son[1],nivel+1);
- }
- }else if(n.L[nivel]=='G'){
- if(root.son[2]==null || equalsNode(root,n)){
- return nivel;
- }
- else {
- return searchNode(n,root.son[2],nivel+1);
- }
- }else if(n.L[nivel]=='T'){
- if(root.son[3]==null || equalsNode(root,n)){
- return nivel;
- }
- else {
- return searchNode(n,root.son[3],nivel+1);
- }
- }else{// do not have to enter.
- return nivel;
- }
- }
- /*public int search(Node n, int len, Node root){
- int number=1;
- for(int i =0; i<len;i++){// for to check all the letters
- if(n.L[i]=='A'){
- if(root.son[0]==n){
- break;
- }
- else
- {
- number=search(n,len,Node root.son[0]);
- }
- }else if(n.L[i]=='D'){
- }else if(n.L[i]=='G'){
- }else if(n.L[i]=='T'){
- }
- }
- return number;
- }*/
- public static void main(String[] args){
- int C = 0; // cases number
- int L = 0; // Length of the chains
- int N = 0; // number of sequences to introduce
- int B = 0; // number of searches to do
- int results[][] = null;
- //ArrayList<Integer> []results =new ArrayList<Integer>()[C];
- ADN adn= null;
- String aux=null;
- String searches= null;
- Scanner s = new Scanner(System.in);
- C= s.nextInt();
- results= new int[C][100];
- for(int i=0;i<C;i++){
- for(int j=0;j<100;j++){
- results[i][j]=-1;
- }
- }
- for(int c=0;c<C;c++){//Cycle for the number of cases
- L=0;
- N=0;
- adn=null;
- aux= null;
- B=0;
- searches=null;
- L= s.nextInt();//maybe we will not use it
- N= s.nextInt();
- adn = new ADN("");//watch out for the root!
- for(int n= 0;n<N;n++){
- aux=s.next();
- adn.addNode(aux);
- }
- B= s.nextInt();
- //results= new int[B];
- /*for(int i=0;i<C;i++){
- results[c][i]= 0;
- }*/
- for(int b= 0; b<B;b++){
- searches=s.next();
- results[c][b]= adn.searchNode(searches);
- }
- }
- for(int c=0;c<C;c++){
- for(int b= 0;b<100;b++){
- if(results[c][b]!=-1)
- System.out.print(results[c][b]+" ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement