78-最长公共前缀

给k个字符串,求出他们的最长公共前缀(LCP)

样例

在 "ABCD" "ABEF" 和 "ACEF" 中, LCP 为 "A"

在 "ABCDEFG", "ABCEFG", "ABCEFA" 中, LCP 为 "ABC"

标签

字符串处理 枚举法 基本实现 LintCode 版权所有

思路

两两比较公共前缀

code

class Solution {
public:
/**
* @param strs: A list of strings
* @return: The longest common prefix
*/
string longestCommonPrefix(vector<string> &strs) {
// write your code here
int size = strs.size(), i = 0, j = 0;
if(size <= 0) {
return string();
}
if(size == 1) {
return strs[0];
} string strA, strB;
strA = strs[0];
for(i=1; i<size; i++) {
strB = strs[i];
string strLCP;
for(j=0; j<strA.size() && j<strB.size(); j++) {
if(strA[j] == strB[j]) {
strLCP += strA[j];
}
else{
break;
}
}
strA = strLCP;
} return strA;
}
};

lintcode-78-最长公共前缀的更多相关文章

  1. lintcode :最长公共前缀

    题目 最长公共前缀 给k个字符串,求出他们的最长公共前缀(LCP) 样例 在 "ABCD" "ABEF" 和 "ACEF" 中,  LCP ...

  2. [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀

    题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...

  3. LeetCode Longest Common Prefix 最长公共前缀

    题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...

  4. 扩展KMP--求字符串S的所有后缀和字符串T的最长公共前缀

    在解上面这个问题前我们要先解决一个类似的问题:求字符串s的所有后缀和s本身的最长公共前缀: 我们用next[]数组保存这些值: 现在我们假设要求next[ x ],并且next[ i ] 0<i ...

  5. BNUOJ34990--Justice String (exkmp求最长公共前缀)

    Justice String Given two strings A and B, your task is to find a substring of A called justice strin ...

  6. [Swift]LeetCode14. 最长公共前缀 | Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  7. [LeeCode]14. 最长公共前缀

    题目链接:https://leetcode-cn.com/problems/longest-common-prefix/ 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀 ...

  8. python(leetcode)-14最长公共前缀

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  9. leetcode-14最长公共前缀

    leetcode-14最长公共前缀 题目 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower& ...

随机推荐

  1. Zabbix——异常问题处理

    报错: zabbix server is not running: the information displayed may not be current 解决: selinux关闭.开启selin ...

  2. spring配置多个视图解析

    <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceVie ...

  3. spring mvc中几种获取request对象的方式

    在使用spring进行web开发的时候,优势会用到request对象,用来获取访问ip.请求头信息等 这里收集几种获取request对象的方式 方法一:在controller里面的加参数 public ...

  4. Docker运行Nginx服务器

    一.获取Docker容器的Nginx镜像 二.创建Docker容器宿主机挂载目录 # 创建挂载目录,-v 显示创建的目录名 [root@idclooknet ~]# mkdir -vp /opt/do ...

  5. vue渲染自定义json数据

    <template> <div class="wrap"> <div class="main"> <div class ...

  6. 一道SQL面试题——表行列数据转换(表转置)

    SQL语句如下: select country, sum(case when type='A' then money end) as A, sum(case when type='B' then mo ...

  7. 使用virtual安装Windows系列操作系统总结

    最近在安装Windows操作系统的过程中,发现总是报错,无法安装成功,后来经过不断地摸索,发现根本的问题在于镜像,所以在以后的大文件传输下载后,一定要校验其MD5值是否与源文件一致,需要的朋友可以联系 ...

  8. java初级应用:环境安装及配置

    相关文件下载: jdk下载路径:http://www.oracle.com/technetwork/java/javase/downloads/index.html eclipse下载路径:https ...

  9. 网站标题被篡改成北京赛车、PK10的解决处理办法

    客户网站于近日被跳转到赌博网站,打开后直接跳转到什么北京赛车,PK10等内容的网站上去,客户网站本身做了百度的推广,导致所有访问用户都跳转到赌博网站上去,给客户带来很大的经济损失,再一个官方网站的形象 ...

  10. count_char

    import java.util.Scanner; public class count_char { public static void main(String args[]) { int cou ...