-
Notifications
You must be signed in to change notification settings - Fork 35
/
MorganandaString.cs
85 lines (73 loc) · 2.04 KB
/
MorganandaString.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
// Morgan and a String
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution {
// Complete the morganAndString function below.
static string morganAndString(string a, string b) {
var result = new StringBuilder(a.Length + b.Length);
var ia = 0;
var ib = 0;
while (ia < a.Length && ib < b.Length)
{
switch (BreakTie(a, b, ia, ib))
{
case -1:
case 0:
var aa = a[ia];
while (ia < a.Length && aa == a[ia])
{
result.Append(aa);
ia++;
}
break;
case 1:
var bb = b[ib];
while (ib < b.Length && bb == b[ib])
{
result.Append(bb);
ib++;
}
break;
}
}
result.Append(a.Substring(ia));
result.Append(b.Substring(ib));
return result.ToString();
}
static int BreakTie (string a, string b, int ja, int jb)
{
while (ja < a.Length && jb < b.Length)
{
if (a[ja] < b[jb])
return -1;
if (a[ja] > b[jb])
return 1;
ja++;
jb++;
}
return ja < a.Length ? -1 : 1;
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int t = Convert.ToInt32(Console.ReadLine());
for (int tItr = 0; tItr < t; tItr++) {
string a = Console.ReadLine();
string b = Console.ReadLine();
string result = morganAndString(a, b);
textWriter.WriteLine(result);
}
textWriter.Flush();
textWriter.Close();
}
}