Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Ejercicio: Algoritmo de Fuerza Bruta
- Autor: Joel Fernandez
- Fecha: 13/06/2014 8:45 p.m.
- IDE: CodeBlocks
- Pagina Web: http://codebotic.blogspot.com
- Descripcion: Implementacion de algoritmo Fuerza Bruta de casamiento de cadenas
- */
- #include<iostream>
- #include <conio.h>
- #include<string.h>
- #include<time.h>
- #define maxPatron 50
- #define maxTexto 500
- using namespace std;
- void FuerzaBruta(char [],char [], int, int); //declaramos la funcion FuerzaBruta
- int main(void)
- {
- system("color 0a");
- char Texto[maxTexto];
- char Patron[maxPatron];
- float Tiempo,inicio,fin;
- int n,m;
- cout<<"\t****************************************************\n";
- cout<<"\t***\t\tALGORITMO FUERZA BRUTA \t\t ***\n";
- cout<<"\t****************************************************\n\n\n";
- cout<<"INGRESE TEXTO:";
- gets(Texto);
- n=strlen(Texto);
- do{ cout<<"\n\nINGRESE PATRON:";
- cin>>Patron;
- m=strlen(Patron);
- if( m > n )
- cout<<"'\n\n\t\t#Error# El texto debe ser mayor o igual que el Patron. \n\n";
- }while(m>n); //validamos que el patron sea menor que el texto
- inicio=clock(); //inicio del tiempo
- FuerzaBruta(Texto,Patron,n,m); //llamamos a la funcion FuerzaBruta
- fin=clock(); //captura el tiempo realizado hasta esta linea
- Tiempo=((fin-inicio)/CLOCKS_PER_SEC);//calculamos el tiempo
- cout<<"\n\nTIEMPO:"<<Tiempo<<endl;
- getch();
- return 0;
- }
- /////////FUERZA BRUTA//////////////
- void FuerzaBruta(char texto[],char patron[], int n, int m)
- { int i,j,k, cont=0;
- char temp[100];
- for(i=0; i<=n;i++)
- { for(j=i,k=0;j<m;j++,k++)
- temp[k]=texto[i+k];
- temp[k]='\0';
- if(strcmp(patron,temp)==0)
- {cout<<"\nCASAMIENTO EN LA POSICION : "<<i<<"\n";
- cont++;}
- m++;
- }
- cout<<"\n\nOCURRENCIAS :"<<cont<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement