Advertisement
Guest User

Untitled

a guest
May 5th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4. struct M {int lung, inizioT; M(int a=0, int b=-1){lung=a; inizioT=b;}};
  5.  
  6. int inizio(int*T,int dimT,int* P,int dimP, int i,int n){
  7. //PRE=(dimT>=0, dimP>=0, i=0, n>=0)
  8. if(dimT==0)//caso base
  9. return i;
  10.  
  11. if(T[0]==P[0])
  12. i=n;
  13.  
  14.  
  15. inizio(T+1,dimT-1,P,dimP,i,n+1);
  16. }
  17. //POST=(i sarà uguale a l'ultimo match di P in T[0...dimT-1]
  18. //la funzione non è corretta restituisce l'ultimo match e non il miglior match,
  19. //con appositi test di verifica restituirebbe erroneamente anche un presunto
  20. // non valido match di lunghezza >=1, che non corrisponde al miglior match.)
  21.  
  22. int val_match(int*T,int dimT,int* P,int dimP, int i){
  23.  
  24. if(dimP==0||dimT==0)//caso base
  25. return i;
  26.  
  27. if(T[0]==P[0])//continua finche' uguale
  28. val_match(T+1,dimT,P+1,dimP-1,i+1);
  29.  
  30. else
  31. return i;
  32. }
  33. //POST=(i=numero max match trovato in T[0...dimP-1])
  34.  
  35. M match(int*T,int dimT,int* P,int dimP, int n){
  36.  
  37. //Pre=(dimT>=0, dimP>=0, n=0)
  38.  
  39. if(dimT==0)//caso base
  40. return M();
  41.  
  42. if(*T==*P){//T[0]==P[0]
  43. int a=1;M x;
  44. x.inizioT=inizio(T,dimT,P,dimP,0,n);
  45. x.lung=val_match(T+1,dimT-1,P+1,dimP-1,a);
  46. return (x);
  47.  
  48. }
  49. else
  50. match(T+1,dimT-1,P,dimP,n+1);
  51. }
  52. //POST=(dopo aver analizzato T[0..dimT-1] e P[0..dimP-1] ritorna x
  53. // and x.inizioT=ultimo best match trovato in T[0...dimT-1]
  54. // and x.lung=massimo match presente di P in T.)
  55.  
  56.  
  57. main()
  58. {
  59. fstream IN("input");
  60. int T[200]={}, P[20]={}, dimT, dimP;
  61. IN>>dimT;
  62. for(int i=0; i<dimT;i++)
  63. IN>>T[i];
  64. IN>>dimP;
  65. for(int i=0; i<dimP;i++)
  66. IN>>P[i];
  67. M x=match(T,dimT, P, dimP, 0);// funzione ricorsiva da fare
  68.  
  69. cout<<"[lung="<<x.lung<<" inizioT="<<x.inizioT<<']'<<endl; // e' l'occasione di ridefinire << per M
  70. cout<<"end"<<endl;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement