Algorithm

Cos Pro 1๊ธ‰ | ๋ฌธ์ž์—ด ํ•ฉ์„ฑํ•˜๊ธฐ, ์™„์ „ ํƒ์ƒ‰

osean 2020. 12. 10. 01:07

๋ฌธ์ œ

 

Arori | ๋‹น์‹ ์˜ ์ง€์‹

ํšŒ์›๊ฐ€์ž… ์กฐ๊ฑด์ด ๋งž๋Š”์ง€ ๋‹ค์‹œํ•œ๋ฒˆ ํ™•์ธํ•ด์ฃผ์ƒˆ์š”

www.sysout.co.kr

๋‘ ๋ฌธ์ž์—ด s1๊ณผ s2๋ฅผ ๋ถ™์—ฌ์„œ ์ƒˆ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œํ•œ ๋ฌธ์ž์—ด์˜ ๋๊ณผ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘์ด ๊ฒน์นœ๋‹ค๋ฉด ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์€ ํ•œ ๋ฒˆ๋งŒ ์ ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด s1 = 'ababc' / s2 = 'abcdab'์ผ ๋•Œ์•„๋ž˜์™€ ๊ฐ™์ด s1 ๋’ค์— s2๋ฅผ ๋ถ™์ด๋ฉด ์ƒˆ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 9์ž…๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ s2 ๋’ค์— s1์„ ๋ถ™์ด๋ฉด ์ƒˆ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 8๋กœ ๋” ์งง๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‘ ๋ฌธ์ž์—ด s1๊ณผ s2๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ s1๊ณผ s2๋ฅผ ๋ถ™์—ฌ์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž์—ด ์ค‘ ๊ฐ€์žฅ ์งง์€ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ return ํ•˜๋„๋ก solution ๋ฉ”์†Œ๋“œ๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.


๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค๋ช…

๋‘ ๋ฌธ์ž์—ด s1๊ณผ s2๊ฐ€ solution ๋ฉ”์†Œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

  • s1๊ณผ s2์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • s1๊ณผ s2๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

return ๊ฐ’ ์„ค๋ช…

s1๊ณผ s2๋ฅผ ๋ถ™์—ฌ์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž์—ด ์ค‘๊ฐ€์žฅ ์งง์€ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ return ํ•ด์ฃผ์„ธ์š”.


์˜ˆ์‹œ

s1 s2 return
"ababc" "abcdab" 8

 

์˜ˆ์‹œ ์„ค๋ช…

๋ฌธ์ œ์— ์ฃผ์–ด์ง„ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

ํ’€์ด

์„ค๋ช…

s1์˜ ๋’ท ๋ถ€๋ถ„๊ณผ s2์˜ ์•ž ๋ถ€๋ถ„์˜ ๋ฌธ์ž์—ด์ด ๊ฐ™์„ ๊ฒฝ์šฐ ํ•ฉ์„ฑ๋œ๋‹ค. ๋‚˜๋Š” ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ***์™„์ „ ํƒ์ƒ‰***์œผ๋กœ ํ’€์—ˆ๋‹ค.

  1. ๋ฌธ์ž์—ด s1 ์„(๋ฅผ) ๋’ค์ง‘์€ ํ›„ char ํƒ€์ž…์œผ๋กœ ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค.
  2. ๋’ค์ง‘์€ ๋ฌธ์ž์—ด s1๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ์‹คํ–‰ํ•ด s2์˜ ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋น„๊ตํ•œ๋‹ค.
    • ๋งŒ์•ฝ ์ผ์น˜ํ•˜๋Š” ์ธ๋ฑ์Šค๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ s2์˜ ํ˜„์žฌ ์ธ๋ฑ์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฐ ๋ฌธ์ž์—ด์˜ ํ˜„์žฌ ์œ„์น˜ํ•œ ์ธ๋ฑ์Šค์˜ ๋‹ค์Œ ์ธ๋ฑ์Šค๊ฐ€ ๋งž๋Š”์ง€ ํƒ์ƒ‰ํ•œ๋‹ค.

์ฝ”๋“œ

package ๋ชจ๋ฐ”์ผ๋ฆฌ๋”_์ฝ”๋”ฉํ…Œ์ŠคํŠธ_03;

public class question_04 {
	public int solution(String s1, String s2) {
		// s1 ๋ฌธ์ž์—ด์„ ๋’ค์ง‘์–ด ๋ฐฐ์—ด๋กœ ์ €์žฅํ•œ๋‹ค.
		char[] arr1 = new char[s1.length()];
		char[] arr2 = s2.toCharArray();
		int t = 0;
		for (int i = s1.length() - 1; i > -1; i--) {
			arr1[t] = s1.charAt(i);
			t++;
		}

		// ๋’ค์ง‘ํžŒ s1์˜ 0 ๋ฒˆ์งธ ์ธ๋ฑ์Šค์™€ s2์˜ i ๋ฒˆ์งธ ์ธ๋ฑ์Šค๊ฐ€ ๊ฐ™์•„์•ผ
		// s1์˜ 1... ๋ฒˆ์งธ์™€ s2์˜ i + 1 ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋‹ค.
		int count = 0;
		for (int idx = 0; idx < arr2.length; idx++) {
			// s2 idx ๋ฒˆ์งธ ์ธ๋ฑ์Šค ์ค‘ ๋’ค์ง‘ํžŒ s1์˜ 0 ๋ฒˆ์งธ ์ธ๋ฑ์Šค์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ๋Š”๋‹ค.
			if (arr1[0] == arr2[idx]) {
				System.out.println("arr1[" + 0 + "] = " + arr1[0] + " / arr2[" + idx + "] = " + arr2[idx]);
				int k = 1;
				// ์œ„์˜ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋‹ค๋ฉด
				for (int i = idx - 1; i > -1; i--, k++) {
					// s2์˜ ์ธ๋ฑ์Šค๋ฅผ ๋‹ค์‹œ ์—ญ์ˆœ์œผ๋กœ ํƒ์ƒ‰ํ•œ๋‹ค.
					// s1์˜ ์ธ๋ฑ์Šค๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ํƒ์ƒ‰ํ•œ๋‹ค.
					if (k < arr1.length) {
						count++;
						System.out.println("arr1[" + k + "] = " + arr1[k] + " / arr2[" + i + "] = " + arr2[i]);
						if (arr1[k] != arr2[i]) {
							break;
						}
					}
				}
				break;
			}
		}
		count += 1;

		StringBuffer sb = new StringBuffer();
		sb.append(s1);
		sb.append(s2.substring(count));
		System.out.println("ํ•ฉ์„ฑ ๊ฐ€๋Šฅ ์ธ๋ฑ์Šค ๊ฐœ์ˆ˜ = " + count);
		System.out.println("๋ฌธ์ž์—ด ํ•ฉ์„ฑ ๊ฒฐ๊ณผ = " + sb.toString());
		int answer = sb.toString().length();
		return answer;
	}

	// ์•„๋ž˜๋Š” ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์ถœ๋ ฅ์„ ํ•ด๋ณด๊ธฐ ์œ„ํ•œ main ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค.
	public static void main(String[] args) {
		question_04 sol = new question_04();
		String s1 = new String("ababcz");
		String s2 = new String("zabcdab");
		int ret = sol.solution(s1, s2);

		// [์‹คํ–‰] ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ถœ๋ ฅ ๊ฐ’์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
		System.out.println("solution ๋ฉ”์†Œ๋“œ์˜ ๋ฐ˜ํ™˜ ๊ฐ’์€ " + ret + " ์ž…๋‹ˆ๋‹ค.");
	}
}