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.0485 sec