U E D R , A S I H C RSS

Data Structure/String

�� �������� 패�����������

~cpp 
int nfind(char *strstr,char *ptnptn)
{
	int str_len=strlen(str);    // �������� ����
	int ptn_len=strlen(ptn);	// 패��� ����
	
	int str_count=0;			// ����
	int ptn_count=0;

	while(1)
	{
		if(strstr[str_count]==ptnptn[ptn_count])	// ���� ��해����
		{
			ptn_count++;		// 패� ��트�� ��키�� � 한�� ��
			str_count++;
		}
		else
		{
			if(ptn_count==0)
				str_count++;
			ptn_count=0;		// ������ 패� 0�� 
		}

		if(ptn_count==ptn_len)  // 패��� ������ ���� ���� �� ���� ���� 
			return str_count-ptn_len+1;

		else if(str_count==str_len)	// 패��� ������ ���� ���� ��(�� �������� ��트 
			return -1;		//  �� ����떄
	}
}

  • ������해������ ��������. �� ���� ���������� 하��..-.-;; ���� �� ���� ��히�� ���� ���� �� �� ���� ���������� �������� 합����.
  • Time Complexity�� O(mn) ������.(m=�������� ����, n=패��� ����)
  • �������� �� �� ������ Failure Function ���� ������ ����.(O(m+n))
  • ���� ����..^^;
    • Failure Function �� �� ? �� �� �� �� �� ������ ��히�� ��������. f(j)���� ���� �� ����������? -Leonardong
      • Failure Function
        j 0 1 2 3 4 5 6 7 8 9
        pattern a b c a b c a c a b
        f(j) -1 -1 -1 0 1 2 3 -1 0 1

      • f(j) = largest i such that i < j and 문자열의 0 ~ i번째 = 문자열의 (j - i) ~ j번째, if such an i exists

        f(j) = -1(Otherwise)

        j = 5 ��, pattern = abcabc ����abcabc���� i = 2���� i < j이므로 f(5) = 2

        j = 4 ��, pattern = abcab ����abcab���� i = 1���� i < j 이므로 f(4) = 1

        j = 3 ��, pattern = abca ����abca���� i = 0���� i < j 이므로 f(3) = 0

        �� ���� ���� ���� - abcabcac f(j) = -1 -- ��
        ������.^^�� C�� fail() ��� �� ����하��. ㅡㅡ; --Leonardong


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:05
Processing time 0.0440 sec