Advertisement
xSiRON

Stringhe con ugual prefisso

Nov 28th, 2015
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. /*************************************
  2.    Nome: Alessio Sferro
  3.    Titolo: StrPrefisso
  4.    Data: 28/11/2015
  5. *************************************/
  6.  
  7. /*
  8.     Esercizio 1:
  9.         Data una matrice di stringhe dire se esiste una colonna in cui siano presenti
  10.         due stringhe con lo stesso prefisso di lunghezza 3.
  11. */
  12.  
  13. #include <iostream>
  14. #include <cstdlib>
  15. #include <ctime>
  16.  
  17. using namespace std;
  18.  
  19. const int DIM = 3;
  20.  
  21. bool checkMatrix(string A[][DIM]);
  22.  
  23. int main(){
  24.     srand(time(NULL));
  25.     string matStr[DIM][DIM];
  26.  
  27.     int x = 0, j = 0, y, k;
  28.  
  29.     cout << "Matrice A:" << endl;
  30.     while(x < DIM){
  31.         y = 0;
  32.         while(y < DIM){
  33.             j = 0;
  34.             k = rand()%7+4;
  35.             while(j < k){
  36.                 matStr[x][y] += char(rand()%2+97);
  37.                 j++;
  38.             }
  39.             cout << "A[" << x << "][" << y << "] = " << matStr[x][y] << endl;
  40.             y++;
  41.         }
  42.         x++;
  43.     }
  44.  
  45.     if(checkMatrix(matStr))
  46.         cout << "\nSUCCESS: stringhe PRESENTI!" << endl;
  47.     else
  48.         cout << "\nFAIL: stringhe non presenti..." << endl;
  49.  
  50.     cout << "\nPremi INVIO per continuare...";
  51.     cin.get();
  52.     return 0;
  53. }
  54.  
  55. bool checkMatrix(string A[][DIM]){
  56.     int y = 0, x, k;
  57.     while(y < DIM){
  58.         x = 0;
  59.         while(x < DIM){
  60.             k = x+1;
  61.             while(k < DIM){
  62.                 if(A[x][y].substr(0, 3) == A[k][y].substr(0, 3)){
  63.                     cout << "\nIndice della colonna in cui sono presenti le stringhe: " << y << endl;
  64.                     return true;
  65.                 }else k++;
  66.             }
  67.             x++;
  68.         }
  69.         y++;
  70.     }
  71.     return false;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement