Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

You have the following 3 operations permitted on a word:

a) Insert a character b) Delete a character c) Replace a character

思路:动态规划。 
                       D[i+1][j+1] = D[i][j];                                             word1[i] == word2[j],
 D[i+1][j+1] = min(min(D[i][j+1], D[i+1][j]), D[i][j]) + 1;              otherwise.

class Solution {
public:
int minDistance(string word1, string word2) {
vector<vector<int> > D(word1.size()+1, vector<int>(word2.size()+1, 0));
for(int j = 0; j <= word2.size(); ++j)
D[0][j] = j;
for(int i = 0; i <= word1.size(); ++i)
D[i][0] = i;
for(int i = 0; i < word1.size(); ++i) {
for(int j = 0; j < word2.size(); ++j) {
if(word1[i] == word2[j])
D[i+1][j+1] = D[i][j];
else D[i+1][j+1] = min(min(D[i][j+1], D[i+1][j]), D[i][j]) + 1;
}
}
return D[word1.size()][word2.size()];
}
};

Simplify Path

Given an absolute path for a file (Unix-style), simplify it.

For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c"

click to show corner cases.

Corner Cases:
  • Did you consider the case where path = "/../"? In this case, you should return "/".
  • Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/". In this case, you should ignore redundant slashes and return "/home/foo".

注意: /...,    /.home,   /..h2me,   /ho_Me/...   为合法路径。

思路: 从头往尾读: 如是 / 和字符,数字,下划线 , 好判断。若是 '.', 则分情况即可。

inline bool isAlpha(char ch) {
return(('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z'));
}
inline bool isAlphaOrUnderline(char ch) {
return isAlpha(ch) || (ch == '_');
}
inline bool isValid(char ch) {
return isAlphaOrUnderline(ch) || ('0' <= ch && ch <= '9');
}
class Solution {// the first alpha should be '/'
public:
string simplifyPath(string path) {
string ans;
path.insert(0, 1, '/'); // but without this state. is OK too.
for(size_t i = 0; i < path.size(); ++i) {
if(path[i] == '.') {
if(i < path.size()-1 && isAlphaOrUnderline(path[i+1])) { ans.push_back('.'); continue;}
else if(i < path.size()-2 && path[i+1] == '.' && (isAlphaOrUnderline(path[i+2]) || path[i+2] == '.')) {
i += 2;
ans.insert(ans.size(), 2, '.');
ans.push_back(path[i]);
continue;
}
}
if(path[i] == '/' && !ans.empty() && ans.back() == '/') continue;
if('0' <= path[i] && path[i] <= '9' && !ans.empty() && ans.back() == '/') continue;
if(path[i] == '/' || isValid(path[i])) ans.push_back(path[i]);
else if(path[i] == '.' && i < path.size()-1 && path[i+1] == '.') {
++i;
if(ans.size() > 1 && ans.back() == '/') ans.pop_back();
while(!ans.empty() && ans.back() != '/') ans.pop_back();
}
}
if(ans.size() > 1 && ans.back() == '/')ans.pop_back();
return ans;
}
};

56. Edit Distance && Simplify Path的更多相关文章

  1. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  2. Min Edit Distance

    Min Edit Distance ----两字符串之间的最小距离 PPT原稿参见Stanford:http://www.stanford.edu/class/cs124/lec/med.pdf Ti ...

  3. eclipse调试(debug)的时候,出现Source not found,Edit Source Lookup Path,一闪而过

    问题描述 使用Eclipse调试代码的时候,打了断点,经常出现Source not found,网上找了半天,大部分提示点击Edit Source Lookup Path,添加被调试的工程,然而往往没 ...

  4. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  5. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  6. Edit Distance

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  7. 编辑距离——Edit Distance

    编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...

  8. LintCode Edit Distance

    LintCode Edit Distance Given two words word1 and word2, find the minimum number of steps required to ...

  9. stanford NLP学习笔记3:最小编辑距离(Minimum Edit Distance)

    I. 最小编辑距离的定义 最小编辑距离旨在定义两个字符串之间的相似度(word similarity).定义相似度可以用于拼写纠错,计算生物学上的序列比对,机器翻译,信息提取,语音识别等. 编辑距离就 ...

随机推荐

  1. 【56测试】【字符串】【dp】【记忆化搜索】【数论】

    第一题:神秘大门 大意: 两个字符串A,B,按字典序最大的顺序输出B 的每个字符在A 中的位置,如果B不全在A中,输出No,否则Yes. 解: 这道题就是一遍的扫描,因为要按字典序最大的输出,所以从后 ...

  2. 在 Vultr VPS 中 以 Debian 8 i386 (jessie) 为 操作系统 平台 手动 搭建 PPTP VPN 全过程

    更新服务器并安装 PPTP 服务  apt-get update apt-get upgrade apt-get install pptpd 编辑 /etc/pptpd.conf 找到 #locali ...

  3. 黑马程序员——OC语言Foundation框架 (2) NSArray NSSet NSDictionary\NSMutableDictionary

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)NSArray 1>NSArray :不可变数组 ①创建方法 ...

  4. HRBUST 1867 差分+BIT

    我在群上看到的某道题,貌似用的是线段树,因为前几天遇到差分,再用BIT动态维护一下前缀和,感觉可做就A了. 加了个读优就Rank1啦! 某个不常见的题库,还是把题目拿下来把.. Description ...

  5. asp.net将页面内容按需导入Excel,并设置excel样式,下载文件(解决打开格式与扩展名指定的格式不统一的问题)

    //请求一个excel类 Microsoft.Office.Interop.Excel.ApplicationClass excel = null; //创建 Workbook对象 Microsoft ...

  6. Windows上管理远程Linux VPS/服务器文件工具 - winscp

    Linux上经常会经常需要编辑文件,特别是Linux VPS/服务器安装好系统之后配置环境会需要修改很多的配置文件等,对于常用Linux的基本上都能够熟练使用vi或者nano等SSH下面的文件编辑工具 ...

  7. EntityFramework 实体映射到数据库

    EntityFramework实体映射到数据库 在Entity Framework Code First与数据表之间的映射方式实现: 1.Fluent API映射 通过重写DbContext上的OnM ...

  8. MetaPhlAn 2:宏基因组进化分析

    描述 MetaPhlAn是分析从物种水平分辨率宏基因组鸟枪法测序数据的微生物群落(细菌,古细菌,真核细胞和病毒)的组成的计算工具.从版本2.0,MetaPhlAn还能够确定具体的菌株(在将样品含有先前 ...

  9. html5本地存储的解决

    1.解决了Cookie  4K存储大小的问题2.解决了请求头常带存储信息的问题3.解决了关系型存储的问题4.跨域问题,跨浏览器*在 HTML5 中,数据不是由每个服务器请求传递的,而是只有在请求时使用 ...

  10. poj3160 强连通+记忆化搜索

    题意:有一张 n 点 m 边的有向无环图,每个点有各自的权值,可正可负,现在从一个点开始走,一直走到不能走到其他点为止,每经过一个点,可以选择获得或不获得它的权值,每个点可以走多次,但是权值只能获得一 ...