<C++코드>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
using namespace std;
void getstring(string parent,string pattern)
{
    int parentsize=parent.size();
    int patternsize=pattern.size();
    int hasparent=0;
    int haspattern=0;
    int power=1;
    for(int i=0;i<=parentsize-patternsize;i++)
    {
        if(i==0)
        {
            for(int j=0;j<patternsize;j++)
            {
                hasparent+=parent[patternsize-1-j]*power;
                haspattern+=pattern[patternsize-1-j]*power;
                if(j<patternsize-1) power*=2;
                
            }
        }
        else
        {
            hasparent=2*(hasparent-parent[i-1]*power)+parent[patternsize+i-1];
            cout<<hasparent<<" "<<haspattern<<endl;
        }
        if(hasparent==haspattern)
        {
        
        bool find=true;
        for(int j=0;j<patternsize;j++)
        {
            if(parent[i+j]!=pattern[j])
            {
                find=false;
                break;
            }
        }
        if(find)
        {
            cout<<i<<"번째에서 일치 합니다"<<' ';
            
        }
    }
    }
    
}
int main(void)
{
    string parent="hellow world";
    string pattern="world";
    getstring(parent,pattern);
    
    return 0;
}
cs

<결과 화면>

+ Recent posts