Me and a friend are working on a project that requires us to create a C++ program (that isnt object oriented) that reads and searches for this pattern of 28 bits in a specific txt file of 2000 bits. Once it finds a full pattern match, it should assign the beginning bit position of the match. e.g. "Match occurs starting at bit position 43".
One constraint: assume no overlapping of pattern matches.
I am just beginning to learn algorithms for C++, so i have asked for help in many places. Any and all help is greatly appreciated.
Edit: to clarify, i am not asking anyone to write this for me. I just want help in regards on where to start, What algoritm you think best fits my case, etc.
or the c equivalent would be what interester mentioned. Though if you do the c way you will have to find the index based on the pointer. Maybe subtract the value of the first position from that.
constchar bitpattern[] = {"1011101111101111011000110011"};
LET occurence_start = 0
start:
WHILE getnextchar() != bitpattern[0]
DO
increment occurence_start
END
FOR k in 1 to len(bitpattern)
DO
IF getnextchar() == bitpattern[k] then
DO
increment k
ELSE
increment occurence_start
SET k = 0
GOTO start
END IF
END
print (Match occurs starting at bit position " + occurence_start)
occurence_start += k
GOTO start
It might need a bit of tweaking to make it linear, but this should find all matches without requiring extra space. In the worst case it is not to far from linear time because of the small subset that is being searched (O(kN)), but if the subset or the search area grows larger, this algorithm might be approaching quadratic time in the worst case. i.e. N2.
#include <ctime>
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
usingnamespace std;
void readFromSearchFile(int data1[], int data2[])
{
ifstream myfile ("charbits.txt");
if (myfile.is_open())
{
int i = 0;
while ( myfile.good() && i<28)
{
data1[i] = myfile.get() - '0';
i++;
}
i=i-28;
while ( myfile.good() )
{
data2[i] = myfile.get() - '0';
i++;
}
myfile.close();
}
else cout << "Unable to open file";
}
int main()
{
clock_t t1, t2;
//Input initializations
int data[2000];
int key[28];
int result[40];
for(int i=0; i<40; i++)
{
result[i]=-1;
}
readFromSearchFile(key,data);
t1=clock();
for(longint i = 1; i<=1000000; i++)
{
//routine
int k=0;
int a=0;
for(int j=0; j<1972; j++)
{
if(data[j]==key[0])
//while(data[j]==key[k])
//{
// k++;
// j++;
//}
if(k>27)
{
result[a]=j-27;
a++;
j=j-1;
}
else
{
j=j-k;
}
k=0;
}
}
t2=clock();
//output
int j=0;
while(result[j]!=-1)
{
cout << "Match occurs starting at bit position " << result[j] << "\n";
j++;
}
cout << "Time difference is " << (t2-t1)/CLK_TCK << " " << " microseconds.";
return 0;
}
This compiles in 16 seconds. Now i just need it to go faster.
Smac89, i'll start playing with that code and see if i can get a faster result
If anyone wants to try it themselves, here's the data in the text file, where the first 28 bits are the pattern itself (name the text file charbits.txt):
If you are trying to squeeze out the last bit of performance, you might want to look at the Boyer-Moore algorithm. It might give you a slight improvement.
With binary strings (as opposed to strings using a larger set of characters) it may not give you much improvement. And the string::find() function may use Boyer-Moore anyway (I have no idea).