111,092
社区成员




题目详情
如果字符串str3能够由str1和str2中的字符按顺序交替形成,那么称str3为str1和str2的交替字符串。例如str1="abc",str2="def",那么"adbecf", "abcdef", "abdecf", "abcdef", "adefbc"等等都为str1和str2的交替字符串。更形式化的,str3的生成算法如下:
str3=""
while str1不为空 or str2不为空:
把str1或str2的首字符加入到str3,并从str1或str2中删除相应的字符
end
给定str1, str2,和str3,判断str3是否为str1和str2的交替字符串。
输入格式:
多组数据,每组数据三行,分别是str1,str2,str3。str1,str2的长度在[1..100]范围内,str3的范围在[1..200]范围内。字符串只包含小写英文字母。
输出格式:
每组数据输出一行YES或者NO。
答题说明
输入样例
a
b
ab
a
b
ca
输出样例:
YES
NO
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
public class Test
{
static bool foo(string s1, string s2, string s)
{
char[] d1 = s1.ToCharArray();
char[] d2 = s2.ToCharArray();
char[] d = s.ToCharArray();
int i1 = 0;
int i2 = 0;
if (d1.Length + d2.Length != d.Length) return false;
for (int i = 0; i < d.Length; i++)
{
if (i1 < d1.Length && d[i] == d1[i1])
{
i1++;
}
else
{
if (i2 < d2.Length && d[i] == d2[i2])
{
i2++;
}
else
{
return false;
}
}
}
return (i1 == d1.Length && i2 == d2.Length);
}
public static String getInfo(String str)
{
string[] data = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
List<string> result = new List<string>();
for (int i = 0; i < data.Length / 3; i++)
{
string s1 = data[i * 3];
string s2 = data[i * 3 + 1];
string s3 = data[i * 3 + 2];
result.Add(foo(s1, s2, s3) ? "YES" : "NO");
}
return string.Join("\r\n", result);
}
public static void Main(string[] Args)
{
if (Args.Length == 0) Args = new string[] { "" };
Console.WriteLine(getInfo(Args[0]));
}
}
class Program
{
static void Main(string[] args)
{
var cin = Console.In;
var cout = Console.Out;
while(cin.Peek() > 0)
{
var line = cin.ReadLine();
cout.WriteLine(line);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
public class Test
{
static bool foo(string s1, string s2, string s)
{
char[] d1 = s1.ToCharArray();
char[] d2 = s2.ToCharArray();
char[] d = s.ToCharArray();
int i1 = 0;
int i2 = 0;
if (d1.Length + d2.Length != d.Length) return false;
for (int i = 0; i < d.Length; i++)
{
if (i1 < d1.Length && d[i] == d1[i1])
{
i1++;
}
else
{
if (i2 < d2.Length && d[i] == d2[i2])
{
i2++;
}
else
{
return false;
}
}
}
return (i1 == d1.Length && i2 == d2.Length);
}
public static String getInfo(String str)
{
string[] data = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
List<string> result = new List<string>();
for (int i = 0; i < data.Length / 3; i++)
{
string s1 = data[i * 3];
string s2 = data[i * 3 + 1];
string s3 = data[i * 3 + 2];
result.Add(foo(s1, s2, s3) ? "YES" : "NO");
}
return string.Join("\r\n", result);
}
public static void Main()
{
string s1 = "", s2 = "", s3 = "";
string tmp = "";
do
{
s1 = tmp == "" ? Console.ReadLine() : tmp;
s2 = Console.ReadLine();
s3 = Console.ReadLine();
Console.WriteLine(getInfo(s1 + "\r\n" + s2 + "\r\n" + s3));
tmp = Console.ReadLine();
} while (false);
}
}
这样写,也就是只接受一组数据,都超时。using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
public class Test
{
static bool foo(string s1, string s2, string s)
{
char[] d1 = s1.ToCharArray();
char[] d2 = s2.ToCharArray();
char[] d = s.ToCharArray();
int i1 = 0;
int i2 = 0;
if (d1.Length + d2.Length != d.Length) return false;
for (int i = 0; i < d.Length; i++)
{
if (i1 < d1.Length && d[i] == d1[i1])
{
i1++;
}
else
{
if (i2 < d2.Length && d[i] == d2[i2])
{
i2++;
}
else
{
return false;
}
}
}
return (i1 == d1.Length && i2 == d2.Length);
}
public static String getInfo(String str)
{
string[] data = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
List<string> result = new List<string>();
for (int i = 0; i < data.Length / 3; i++)
{
string s1 = data[i * 3];
string s2 = data[i * 3 + 1];
string s3 = data[i * 3 + 2];
result.Add(foo(s1, s2, s3) ? "YES" : "NO");
}
return string.Join("\r\n", result);
}
public static void Main()
{
string s1 = "", s2 = "", s3 = "";
string tmp = "";
do
{
s1 = tmp == "" ? Console.ReadLine() : tmp;
s2 = Console.ReadLine();
s3 = Console.ReadLine();
Console.WriteLine(getInfo(s1 + "\r\n" + s2 + "\r\n" + s3));
tmp = Console.ReadLine();
} while (tmp != "");
}
}
int main() {
string a,b,c;
while (scanf("%s%s%s",s1,s2,s3) != EOF) {
a = s1;
b = s2;
c = s3;
puts(isInterleaving(a,b,c)?"YES":"NO");
}
return 0;
}