Write a program that reads two strings s and t, each on its own line. Each string will contain only lowercase letters. The program should print a string u such that
A subsequence is not necessarily contiguous. If there is more than one longest possible u, print the one that comes first alphabetically.
Sample input:
watermelon
summertime
Output:
eemrt
Sample input #2:
rhinoceros
elephant
Output:
ehn
Hint: If you have an integer i in the range 0 ≤ i < 26 and you want to convert it to a letter from 'a' to 'z', you can do this:
char c = (char) ('a' + i);
是不是你这个测试软件完必须要完全按照给出示例那样,直接输入,输出,不给增加任何其他输出内容的。。我改下逻辑看看。。:~~
我这里VS跑没有问题。。
using System;
using System.Collections.Generic;
using System.Linq;
namespace Test
{
class Program
{
static void Main(string[] args)
{
var s = Console.ReadLine();
var t = Console.ReadLine();
var arrS = s.ToCharArray();
var arrT = t.ToCharArray();
var dit = new Dictionary<char, int>();
foreach (var c in arrS)
{
if (!dit.ContainsKey(c)) dit.Add(c, 0);
dit[c]++;
}
var rst = new List<char>();
foreach (var c in arrT)
{
if (dit.ContainsKey(c))
{
rst.Add(c);
dit[c]--;
if (dit[c] == 0) dit.Remove(c);
}
}
Console.WriteLine(String.Join("", rst.OrderBy(i => i)));
}
}
}
11