Algorithm
[BOJ] 1120 문자열
일등하이
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);
}
}
}

반응형