一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 682|回复: 1
收起左侧

[算法题] 求一题解法(difficult)

[复制链接] |试试Instant~ |关注本帖
woodman 发表于 2016-3-6 13:11:23 | 显示全部楼层 |阅读模式

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

x
本帖最后由 woodman 于 2016-3-6 03:46 编辑

Problem Statement

You are given a list of clues, and all clues have the same length. Some of the letters are missing, and those letters are represented by question marks ('?'). Arrange the clues in a vertical row, such that corresponding characters in each clue are in the same column. Then, order the clues so that each column is sorted in non-decreasing order from top to bottom. You can replace each question mark with any letter to achieve this

Print the new list of clues containing the lexicographically earliest ordering that you can achieve. If there is no way to achieve the goal, print NONE instead. An ordering A comes before an ordering B if A contains an alphabetically earlier clue at the first index where they differ

Input

Line 1: N

Line 2 .. N + 1: clue_i

Output

As stated in the problem statement

Input Explanation

Line 1: N, the number of clues

Line 2 to line N+1: clue_i, uppercase A-Z characters

Output Explanation

Output will contain exactly N lines or NONE if no such ordering is possible

Sample Input

3

?ED?

TO??

????

Sample Output

AAAA

AEDA

TODA
如题  求个java 的解法 求大家指点 多谢了
import java.io.*;
import java.util.*;
class Test {
    public static void main(String [] args) throws Exception{
        BufferedReader in = new BufferedReader(
            new InputStreamReader(System.in));
        String str = in.readLine();

ccarter 发表于 2016-3-8 08:24:01 | 显示全部楼层
说一下算法吧,应该是对的,代码不写了
每个string都当做一个结点,如果对于两个string a和b,存在一个index使得a[i] != '?' && b[i] != '?' && a[i] < b[i],就在a b之间连一条有向边,a指向b。所有的string pair都处理完成后对图进行拓扑排序,排序的结果就是string排好的顺序,把所有?替换成合适的字符即可。如果无法拓扑排序就输出NONE。
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-5 17:22

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

快速回复 返回顶部 返回列表