m0_54132659 2021-03-15 11:10 采纳率: 83.3%
浏览 138
已结题

c #编程题,会的直接留下代码,感谢

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

  1. Some permutation of u is a subsequence of s.
  2. Some permutation of u is a subsequence of t.
  3. u is as long as possible.

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);
  • 写回答

4条回答 默认 最新

  • Go 旅城通票 2021-03-15 14:51
    关注

    是不是你这个测试软件完必须要完全按照给出示例那样,直接输入,输出,不给增加任何其他输出内容的。。我改下逻辑看看。。:~~

     

    我这里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)));
    
    
            }
        }
    
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line