[BOJ] 1120 문자열

Algorithm 2023. 1. 13. 14:03
반응형

https://www.acmicpc.net/problem/1120

1120번: 문자열

길이가 N으로 같은 문자열 X와 Y가 있을 때, 두 문자열 X와 Y의 차이는 X[i] ≠ Y[i]인 i의 개수이다. 예를 들어, X=”jimin”, Y=”minji”이면, 둘의 차이는 4이다. 두 문자열 A와 B가 주어진다. 이때, A의

www.acmicpc.net



[풀이]
A의 길이가 B의 길이보다 작거나 같기 때문에 A를 좌우로 움직여 가면서 빈공간에 앞뒤로 문자를 넣을수 있게 하고
모든 경우에 따라 A와 B의 차이를 구해 최소값을 찾는 문제다


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Study12
{
    class App
    {
        //생성자 
        public App()
        {
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 6; j++) {
                    Console.WriteLine("{0}, {1}", i, i + j);
                }
                Console.WriteLine("----------");
            }

        }
    }
}

using System;

namespace Study12
{
    class App
    {
        //생성자 
        public App()
        {
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 6; j++) {
                    Console.WriteLine("{0}, {1}", j, i + j);
                }
                Console.WriteLine("----------");
            }

        }
    }
}


using System;

namespace Study12
{
    class App
    {
        //생성자 
        public App()
        {
            string str = "adaabc aababbc";
            var arr = str.Split(' ');
            string a = arr[0];
            string b = arr[1];
            int total = (b.Length - a.Length) + 1;
            for (int i = 0; i < total; i++) {
                for (int j = 0; j < a.Length; j++) {
                    Console.WriteLine("{0}, {1}", a[j], b[i + j]);
                }
                Console.WriteLine("----------");
            }

        }
    }
}

using System;

namespace Study12
{
    class App
    {
        //생성자 
        public App()
        {
            string str = "adaabc aababbc";
            var arr = str.Split(' ');
            string a = arr[0];
            string b = arr[1];
            int total = (b.Length - a.Length) + 1;
            int min = int.MaxValue; //가장 최소값을 찾아야 하므로 가장 큰수를 넣어 놓는다 
            for (int i = 0; i < total; i++) {
                int cnt = 0;//모든 경우의 최소값을 저장 해야 함 
                //비교가 끝나면 
                for (int j = 0; j < a.Length; j++) {
                    Console.WriteLine("{0}, {1}", a[j], b[i + j]);
                    if (a[j] != b[i + j]) {
                        cnt++;  //A와 B의 차이 
                    }
                }
                //기존에 저장되어 있던 최소 값과 비교 해 더 최소값을 찾아야 함 
                min = Math.Min(min, cnt);
                Console.WriteLine("----------");
            }
            Console.WriteLine("A와 B의 차이 : {0}", min);
        }
    }
}


참고
https://velog.io/@dding_ji/baekjoon-1120

반응형

'Algorithm' 카테고리의 다른 글

[BOJ] 10103 주사위 게임  (0) 2023.01.13
[BOJ] 10974 모든 순열  (0) 2023.01.13
[BOJ] C# 2309 일곱 난쟁이  (0) 2023.01.13
[BOJ] C# 10173 니모를 찾아서  (0) 2023.01.12
[BOJ] 1181 단어정렬  (0) 2023.01.12
: