给定N行只包含大写字母"A-Z"的字条串,消除规则如下:如果同一行中包含长度>1的由相同字母组成的子串,那么这—行的这些子串都会被同时消除,剩下的子串拼接成新的字符串。
给定N行字符串,重复消除M次之后,把消除结果打印出来特别提示:跟传统的消消乐游戏有点不一样,这里的消消乐只考虑行消除,不考虑列消除
输入描述
第一行有两个数字
N M
代表输入一共有N行字符串,进行M次消除,N<=100,M<=50,每行的字符串长度<=1000
后面是N行字符串
输出描述
M次消除后的结果,注意,输出结果也要严格是N行
示例1
输入
2 1
DAABA
AADBB
输出
DBA
D
示例2
输入
4 2
ACBBCAAC
CCAABD
DDAA
CBA
输出
A
BD
CBA
using System;
using System.Collections.Generic;
class Program
{
static List<string> EliminateStrings(List<string> strings)
{
List<string> result = new List<string>();
foreach (string s in strings)
{
string str = s;
int i = 0;
while (i < str.Length)
{
int j = i + 1;
while (j < str.Length && str[j] == str[i])
{
j++;
}
if (j - i > 1)
{
// Eliminate the repeated substring
str = str.Substring(0, i) + str.Substring(j);
}
else
{
i = j;
}
}
result.Add(str);
}
return result;
}
static void Main()
{
// 输入N和M
string[] input = Console.ReadLine().Split();
int N = int.Parse(input[0]);
int M = int.Parse(input[1]);
// 读取N行字符串
List<string> strings = new List<string>();
for (int i = 0; i < N; i++)
{
strings.Add(Console.ReadLine());
}
// 重复消除M次
for (int i = 0; i < M; i++)
{
strings = EliminateStrings(strings);
}
// 打印最终结果
foreach (string s in strings)
{
Console.WriteLine(s);
}
}
}