码迷,mamicode.com
首页 > 其他好文 > 详细

Graph题目总结【不定期更新】

时间:2020-01-22 12:49:47      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:unknown   logic   layout   char   log   Plan   orm   title   str1   

Find minimal ops to convert one str to another

Description Given two alphabet strings str1 and str2. You can change the characters in str1 to any alphabet characters in order to transform str1 to str2. One restriction is, in each operation, you should change all the same characters simultaneously. What‘s more, you may use another special character * if necessary, which means during each operations, you may change the alphabet characters to *, or change each * to a specific character. We want to know the minimum operation times. If impossible, return -1. Template
int MinOpTimes(string str1, string str2) { //...
};
Examples
 
Example 1:
str1: accs, str2: eeec
operation 1: change ‘a‘->‘e‘; // str1: eccs operation 2: change ‘c‘->‘e‘; // str1: eees operation 3: change ‘s‘->‘c‘; // str1: eeec return 3;
Example 2:
str1: accs, str2: efec return -1;
Example 3:
str1: abb , str2: baa
operation 1: change ‘a‘->‘*‘; // str1: *bb operation 2: change ‘b‘->‘a‘; // str1: *aa
operation 3: change ‘*‘->‘b‘; // str1: baa return 3;
 
accs -> eeec: 3
技术图片

 

accs -> efec: -1

技术图片

 

 

 abb -> baa: 3

技术图片

 

 可以看到如果想要convert成功所有的node的out degree要小于等于1才可以。答案是计算edges+cycle的数量。

Alien Dictionary

There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of non-empty words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.

Example 1:

Input: [ "wrt", "wrf", "er", "ett", "rftt" ] Output: "wertf"

Example 2:

Input: [ "z", "x" ] Output: "zx"

Example 3:

Input: [ "z", "x", "z" ] Output: ""  Explanation: The order is invalid, so return "".

Note:

  1. You may assume all letters are in lowercase.

  2. You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.

  3. If the order is invalid, return an empty string.

  4. There may be multiple valid order of letters, return any one of them is fine.

 

Topological sort: similar with course schedule series

e.g. [ "wrt", "wrf", "er", "ett", "rftt" ]

1. wrt -> wrf: t -> f

2. wrt -> er:  w -> e

3. er -> ett: r -> t

4. ett -> rftt: e -> r

技术图片

e.g. [ "z", "x", "z" ] : 有环 -> invalid

技术图片

 

 

 

 

Graph题目总结【不定期更新】

标签:unknown   logic   layout   char   log   Plan   orm   title   str1   

原文地址:https://www.cnblogs.com/goldenticket/p/12221303.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!