问题的本质:输入是:
- 每个输入的行数
- 字符串(0 和 1)
- 对字符串的操作次数(按数字替换元素)
- 操作(格式:“数字”“字符”)
原文:https ://codeforces.com/problemset/problem/2036/C
困难在于时间限制,但是这段代码在测试2中的550代币上是错误的,我不明白问题是什么,请提示逻辑中的漏洞,提前感谢
int n = Convert.ToInt32(Console.ReadLine());
List<int> list1100 = new List<int>();
List<string> list = new List<string>();
bool result;
for (int i = 0; i < n; i++)
{
char[] arr = Console.ReadLine().ToCharArray();
Func(arr);
int q = Convert.ToInt32(Console.ReadLine());
for (int k = 0; k < q; k++)
{
string x = Console.ReadLine();
if (arr.Length < 4)
{
list.Add("NO");
continue;
}
string[] str = x.Split(' ');
int index = Convert.ToInt32(str[0]);
char value = Convert.ToChar(str[1]);
result = list1100.Count > 0;
if (value == arr[index - 1])
{
list.Add(result ? "YES" : "NO");
continue;
}
arr[index - 1] = value;
if (value == '1')
{
if (arr.Length > index + 2) // {input}100
{
if (arr[index] == '1' && arr[index + 1] == '0' && arr[index + 2] == '0') { if (!list1100.Contains(index - 1)) { list1100.Add(index - 1); } list.Add("YES"); continue; }
}
if (arr.Length >= index + 2 && index != 1) // 1{input}00
{
if (arr[index - 2] == '1' && arr[index] == '0' && arr[index + 1] == '0') { if (!list1100.Contains(index - 2)) { list1100.Add(index - 2); } list.Add("YES"); continue; }
}
}
else // value == 0
{
if (arr.Length >= index + 1 && index >= 3) // 11{input}0
{
if (arr[index - 3] == '1' && arr[index - 2] == '1' && arr[index] == '0') { if (!list1100.Contains(index - 3)) { list1100.Add(index - 3); } list.Add("YES"); continue; }
}
if (arr.Length >= index && index >= 4) // 110{input}
{
if (arr[index - 4] == '1' && arr[index - 3] == '1' && arr[index - 2] == '0') { if (!list1100.Contains(index - 4)) { list1100.Add(index - 4); } list.Add("YES"); continue; }
}
}
foreach (int ex in list1100)
{
if (ex <= index - 1 && index - 1 <= ex + 3)
{
list1100.Remove(ex);
break;
}
}
result = list1100.Count > 0;
list.Add(result ? "YES" : "NO");
}
}
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
void Func(char[] arr)
{
list1100.Clear();
for (int j = 0; j < arr.Length - 3; j++)
{
if (arr[j] == '1' && arr[j + 1] == '1' && arr[j + 2] == '0' && arr[j + 3] == '0') { list1100.Add(j); }
}
return;
}