-
Notifications
You must be signed in to change notification settings - Fork 1
/
Beautiful Binary String.cs
155 lines (131 loc) · 3.7 KB
/
Beautiful Binary String.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
public class Solver
{
public void Solve()
{
int n = ReadInt();
var a = ReadToken().ToCharArray();
int ans = 0;
for (int i = 1; i + 1 < n; i++)
if (a[i - 1] == '0' && a[i] == '1' && a[i + 1] == '0')
{
a[i + 1] = '1';
ans++;
}
Write(ans);
}
#region Main
protected static TextReader reader;
protected static TextWriter writer;
static void Main()
{
#if DEBUG
reader = new StreamReader("..\\..\\input.txt");
writer = Console.Out;
//writer = new StreamWriter("..\\..\\output.txt");
#else
reader = new StreamReader(Console.OpenStandardInput());
writer = new StreamWriter(Console.OpenStandardOutput());
//reader = new StreamReader("game.in");
//writer = new StreamWriter("game.out");
#endif
try
{
new Solver().Solve();
}
catch (Exception ex)
{
#if DEBUG
Console.WriteLine(ex);
#else
Console.WriteLine(ex);
throw;
#endif
}
reader.Close();
writer.Close();
}
#endregion
#region Read / Write
private static Queue<string> currentLineTokens = new Queue<string>();
private static string[] ReadAndSplitLine()
{
return reader.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
}
public static string ReadToken()
{
while (currentLineTokens.Count == 0)
currentLineTokens = new Queue<string>(ReadAndSplitLine());
return currentLineTokens.Dequeue();
}
public static int ReadInt()
{
return int.Parse(ReadToken());
}
public static long ReadLong()
{
return long.Parse(ReadToken());
}
public static double ReadDouble()
{
return double.Parse(ReadToken(), CultureInfo.InvariantCulture);
}
public static int[] ReadIntArray()
{
return ReadAndSplitLine().Select(x => int.Parse(x)).ToArray();
}
public static long[] ReadLongArray()
{
return ReadAndSplitLine().Select(x => long.Parse(x)).ToArray();
}
public static double[] ReadDoubleArray()
{
return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
}
public static int[][] ReadIntMatrix(int numberOfRows)
{
int[][] matrix = new int[numberOfRows][];
for (int i = 0; i < numberOfRows; i++)
matrix[i] = ReadIntArray();
return matrix;
}
public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
{
int[][] matrix = ReadIntMatrix(numberOfRows);
int[][] ret = new int[matrix[0].Length][];
for (int i = 0; i < ret.Length; i++)
{
ret[i] = new int[numberOfRows];
for (int j = 0; j < numberOfRows; j++)
ret[i][j] = matrix[j][i];
}
return ret;
}
public static string[] ReadLines(int quantity)
{
string[] lines = new string[quantity];
for (int i = 0; i < quantity; i++)
lines[i] = reader.ReadLine().Trim();
return lines;
}
public static void WriteArray<T>(IEnumerable<T> array)
{
writer.WriteLine(string.Join(" ", array.Select(aa => aa.ToString()).ToArray()));
}
public static void Write<T>(params T[] array)
{
WriteArray(array);
}
public static void WriteLines<T>(IEnumerable<T> array)
{
foreach (var a in array)
writer.WriteLine(a);
}
#endregion
}