[BOJ] 1254 팰린드롬 만들기
Algorithm 2023. 1. 20. 01:42반응형
https://www.acmicpc.net/problem/1254
힌트
이미 팰린드롬일경우
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace _1159
{
class Program
{
static void Main()
{
StreamReader sr = new StreamReader(Console.OpenStandardInput());
//StringBuilder sb = new StringBuilder();
StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
//abab
//a bab a
//ab ab ba
//aba b aba
//abab baba
var str = sr.ReadLine();
var len = str.Length;
var list = new List<string>();
//이미 팰린드롬
if (str == new string(str.Reverse().ToArray()))
{
sw.WriteLine(len);
}
else
{
for (int i = 0; i < len; i ++)
{
var first = str.Substring(0, i + 1);
// sw.Write("{0}\t", first);
var mid = "";
if (len - (i + 1) > 0)
{
mid = str.Substring(i + 1, len - (i + 1));
// sw.Write("{0}\t", mid);
}
// else
// sw.Write("\t");
var last = new string(first.Reverse().ToArray());
// sw.WriteLine("{0}", last);
if (mid != "" && mid == new string(mid.Reverse().ToArray()))
{
list.Add(string.Format("{0}{1}{2}", first, mid, last));
}
//sw.WriteLine(len - (i + 1));
}
// foreach (var ele in list)
// {
// sw.WriteLine("--->{0}" , ele);
// }
//https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.sort?view=net-7.0
list.Sort((x, y) => x.Length.CompareTo(y.Length));
// sw.WriteLine(list[0]);
sw.WriteLine(list[0].Length);
}
sr.Close();
sw.Close();
}
}
}
반응형
'Algorithm' 카테고리의 다른 글
[BOJ] 21921 블로그 (0) | 2023.01.20 |
---|---|
[BOJ] 1940 주몽 (0) | 2023.01.20 |
[BOJ] 1159 농구 경기 (0) | 2023.01.20 |
[BOJ] 1764 듣보잡 (0) | 2023.01.20 |
[BOJ] 1251 단어 나누기 (0) | 2023.01.20 |