#include <iostream>
using namespace std;
class KarpRabin
{
private:
string text;
string pattern;
int textSize;
int patternSize;
int Hash(const string t);
int Rehash(int preHash, int coefficient, int start);
bool subCheck(int i){
for (int j = 0; j < patternSize; j++)
if (text[i + j] != pattern[j])
return false;
return true;
}
public:
KarpRabin(){}
KarpRabin(string t){ text = t; patternSize = 0; textSize = t.length(); }
void Search_Text(string p);
};
int KarpRabin::Hash(const string t)
{
int i = 0,j=0;
int hash = 0;
if (t.length() < patternSize)
return -1;
for (i = (patternSize - 1),j=0; i >= 0; i--,j++){
hash += (t[j]) * (int)(0x01<<i);
}
return hash;
}
int KarpRabin::Rehash( int preHash, int coefficient,int start)
{
return ((preHash - (text[start-1] * coefficient)) * 2) + text[start + patternSize-1];
}
void KarpRabin::Search_Text(string p)
{
int i = 0;
int coeff = 1;
int textHash = 0;
int patternHash = 0;
pattern = p;
patternSize = pattern.length();
coeff <<= patternSize - 1;
patternHash = Hash(pattern);
textHash = Hash(text);
for (i = 1; i <= textSize - patternSize; i++){
if (patternHash == textHash)
if (subCheck(i-1))
cout << i-1 << " : " << pattern.c_str() << endl;
textHash = Rehash(textHash, coeff,i);
}
}
////////////////////////////////////// 메인
#include <iostream>
#include "graph.h"
#include "strinfSearch.h"
#define TXT_BUFFER 512
using namespace std;
int main(int argc,char** argv)
{
char* FilePath;
string pattern;
string text;
FILE* fp;
char buf[TXT_BUFFER];
KarpRabin* k;
if (argc < 3){
FilePath = "input.txt";
pattern = "How";
}
else{
FilePath = argv[1];
pattern = argv[2];
}
if ((fopen_s(&fp,FilePath, "r")) != 0){
cout << "fail open file" << endl;
return 1;
}
while (fgets(buf, TXT_BUFFER, fp) != NULL){
text.append(buf);
}
fclose(fp);
k = new KarpRabin(text);
k->Search_Text(pattern);
return 0;
}
라빈카프 라고만 불러서 한참 뭔지 생각했네
시크님 근데 저 코딩스타일 피드백좀해주시면 안되나요?? 살면서 누가 피드백해준적이없어서....
글쎄.. 그냥 난 }
}else{ 라고 쓰는 편이고 i 같은 반복제어문은 { } 내로 선언해서 그안에서 쓰고 바로 없애는 편이고 출력문은 메인에서만 쓰는편
근뎅 그냥 공부하면서 적은 코드니까 뭐든 괜찮지 않을까 ?
감사해욤 ㅎ,.ㅎ