最长公共前缀

Problem statement:

问题陈述:

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

编写函数以在字符串数组中找到最长的公共前缀字符串

If there is no common prefix, return an empty string "".

如果没有公共前缀,则返回一个空字符串“”

Solution:

解:

Input Example:

输入示例:

    Example 1:
Let the set of strings to be {"flower", "fly", "flow"}
The longest common prefix is "fl"
Example 2:
Let the set of strings to be {"dog", "donkey", "door", "cat"}
The longest common prefix is "", i.e., empty string.
Since there is no common prefix at all.

最长的公共前缀 (Longest common prefix)

Longest common prefix simply means the longest prefix (prefix is a substring also, but not vice-versa) all the member strings consist of.

最长公共前缀仅表示所有成员字符串组成的最长前缀(前缀也是子字符串,反之亦然)。

查找一组字符串的最长公共前缀的算法 (Algorithm to find longest common prefix of a set of strings)

Solving particularly for two string, the problem is not that difficult what it is for a set of strings. But we can simply break the problem into sub-problems where we can solve for only two strings and simply pass the output for further string processing 's. In a simple word, the whole thing can be formulated to be:

特别是对于两个字符串,解决的问题并不像一组字符串那么困难。 但是我们可以简单地将问题分解为几个子问题,在这些子问题中,我们只可以解决两个字符串,然后将输出传递给进一步的字符串处理。 简而言之,整个事情可以表述为:

    LongestCommonPrefix(string1, string2, ..., string n)
=   LongestCommonPrefix(LongestCommonPrefix(string1,string2),string3,...,string n)
=   LongestCommonPrefix(newstring1,string3,string4,...,string n)
=   ...
=   LongestCommonPrefix(newstring n-1, string n)
=   new string n = final result

So this simply converts the problem for a set of
strings to a problem of two strings only

Finding longest common prefix for two strings (string x, string y)

查找两个字符串(字符串x,字符串y)的最长公共前缀

  1. Check for base case

    检查基本情况

  2. IF anyone is empty string
    return empty string;
    
    
  3. Declare result as an empty string;

    将结果声明为空字符串;

  4.     IF string x is smaller than y
    //check for common prefix part on basis of x
    FOR( i=0;i<length of string x; i++)
    IF(x[i]==y[i])
    result+=x[i]; //append the common character
    ELSE//no common character at this point
    Returnresult
    END FOR
    ELSE//if string y is smaller than x
    //check for common prefix part on basis of y
    FOR (i=0;i<length of y; i++)
    IF(y[i]==x[i])
    result+=y[i];//append the common character
    ELSE//no common character at this point
    return result;
    END FOR
    END IF-ELSE
    
    
  5. result consist of longest common prefix for these two strings

    结果由这两个字符串最长公共前缀组成

Explanation with example

举例说明

    Let's consider the first example
The set of strings: {"flower", "fly", "flow"}
LongestCommonPrefix("flower", "fly", "flow")
=   LongestCommonPrefix(LongestCommonPrefix ("flower","fly"),"flow")
=   LongestCommonPrefix("fl", "flow")
=   "fl"
Let's consider the second example
the set of strings to be {"dog", "donkey", "door", "cat"}
LongestCommonPrefix("dog", "donkey", "door", "cat")
=   LongestCommonPrefix(LongestCommonPrefix ("dog", "donkey"),"door", "cat")
=   LongestCommonPrefix("do","door", "cat")
=   LongestCommonPrefix (LongestCommonPrefix("do", "door") , "cat")
=   LongestCommonPrefix("do", "cat")
=   "" //empty string
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

C++ implementation

C ++实现

#include <bits/stdc++.h>
using namespace std;
string findPrefix(string x,string y){//base case checking
if(x=="" || y=="")
return "";
string result="";
//if string x is smaller than y
if(x.length()<y.length()){//check up for common prefix part
for(int i=0;i<x.length();i++){if(x[i]==y[i])
result+=x[i];
}
}
else{//if string y is smaller than x
for(int i=0;i<y.length();i++){//check up for common prefix part
if(y[i]==x[i])
result+=y[i];
else
return result;
}
}
return result;
}
string longestCommonPrefix(vector<string>& strs) {//base cases
if(strs.size()==0)
return "";
//if only one string exists
if(strs.size()==1)
return strs[0];
string prefix=strs[0];
//follow the associative property for checking
//take two strings each time & pass output prefix
//as one string for further processings
for(int i=1;i<strs.size();i++){prefix=findPrefix(prefix,strs[i]);
if(prefix=="")
return prefix;
}
return prefix;
}
int main(){int n;
cout<<"enter no of strings you want to add\n";
cin>>n;
string s;
vector<string> v;
cout<<"enter "<<n<<" strings\n";
//collect input
while(n--){cin>>s;
v.push_back(s);
}
//print longest common prefix
if(longestCommonPrefix(v)=="")
cout<<"no common prefix between the strings\n";
else
cout<<"longest common prefix: "<<longestCommonPrefix(v)<<endl;
return 0;
}

Output

输出量

First run:
enter no of strings you want to add
3
enter 3 strings
flower
fly
flow
longest common prefix: fl
Second run:
enter no of strings you want to add
4
enter 4 strings
dog
donkey
door
cat
no common prefix between the strings

翻译自: https://www.includehelp.com/icp/longest-common-prefix.aspx

最长公共前缀

最长公共前缀_最长的公共前缀相关推荐

  1. hwd分别是长宽高_衣柜长宽高标准尺寸一般是多少 衣柜怎么保养

    一般的话,我们在市面上购买衣柜的时候会考虑尺寸的问题,因为家里房子的大小不相同,所以选择的尺寸也不一样,如果买的太大了家里就放不下,所以,大家一定要看一下衣柜长宽高的标准尺寸一般是多少,大家可以根据标 ...

  2. java实现最长连续子序列_最长公共子序列 ||

    问题:在 前一篇文章 最长公共子序列 | 的基础上要求将所有的最长公共子序列打印出来,因为最长公共子序列可能不只一种. 难点:输出一个最长公共子序列并不难,难点在于输出所有的最长公共子序列,我们需要在 ...

  3. java实现最长连续子序列_最长公共子序列/最长公共子串 Python/Java实现

    关注我的微信公众号:后端技术漫谈 不定期推送关于后端开发.爬虫.算法题.数据结构方面的原创技术文章,以及生活中的逸闻趣事. 我目前是一名后端开发工程师.主要关注后端开发,数据安全,网络爬虫,物联网,边 ...

  4. LeetCode刷题记录_最长公共前缀

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

  5. 20190501-编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串...

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

  6. ac自动机 匹配最长前缀_别再暴力匹配字符串了,高效的KMP,才是真的香

    如果你想了解KMP算法,请静下心读完这篇文章,一定不会辜负你的时间 暴力匹配(BF) 字符串匹配是我们在编程中常见的问题,其中从一个字符串(主串)中检测出另一个字符串(模式串)是一个非常经典的问题,当 ...

  7. LeetCode4_编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 ““。(解决方案:横向扫描、 纵向扫描 、分治 二分查找 、秀儿操作之排序比较头尾)

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

  8. c语言最长公共子序列_序列比对(二十四)——最长公共子序列

    原创: hxj7 本文介绍如何求解两个字符串的最长公共子序列. 最长公共子序列问题 前文<序列比对(二十三)--最长公共子字符串>介绍了如何求解两个字符串的最长公共子字符串,本文将介绍如何 ...

  9. 两个字符串的最长公共子序列长度_输出两个字符串的最长公共子串和最长公共子序列...

    输出两个字符串的最长公共子串和最长公共子序列.求解两个字符串的最长公共子串和最长公共子序列在方法上很接近,都是动态规划.只不过在递推方程上有一些不一样. 求两个字符串的最长公共子串 #include ...

最新文章

  1. Java三大主流框架概述--(转载)
  2. 你还在使用 try-catch-finally 关闭资源?不太优雅~
  3. docker java 不兼容_Apple M1 芯片不支持 Docker?Docker:正在努力适配
  4. c语言现代方法15章答案(自己做的,更新中)
  5. eclipse 代码提示后面的百分比是什么意思?
  6. 软件测试工程师简历经验总结:软件测试工程师简历项目经验怎么写?(转载)
  7. 利用高德地图获取经纬度-python
  8. Windows10 如何禁用或删除大的Hiberfil.sys和Pagefile.sys文件
  9. 项目实训个人报告(二)
  10. c++ 去除字符串首尾的空白字符
  11. 互联网早报:腾讯推出“微小号”,用虚拟手机号拨打电话收发短信
  12. 模型预测控制器(MPC)系列: 1.建立车辆横向动力学模型
  13. RFID标签测试结果受哪些因素影响
  14. Android 恢复出厂设置时间重置
  15. 神经网络控制系统的应用,中枢神经信息网络系统
  16. 幻方5*5(java实现)
  17. joinquant量化是什么?是主流的量化平台吗?
  18. Linux 电池 驱动
  19. Twitter常用术语和名词解析
  20. 英语自我介绍计算机专业,计算机专业面试英文自我介绍范文

热门文章

  1. Spring根据包名获取包路径下的所有类
  2. android 空白占位符,android textview空格占位符以及一些其他占位符汇总
  3. linux dlopen 内存,Linux下加载库的有关问题(dlopenm, dlsym)
  4. android获取图片方向并旋转,Android 判断imageview角度并旋转
  5. linux恢复终端默认配置,以gnome-terminal为例,修改gnome3 的默认配置,
  6. 导入数据中文乱码_基于Navicat和Kettle的数据迁移完全解读(多图)
  7. flask ajax 文件上传,python flask使用ajax请求上载文件。文件为空
  8. excel 区间人数柱状图_Excel中,区间统计的3种技巧都不掌握,那就真的OUt了!
  9. Angular之ngx-permissions的控制视图访问
  10. 「作文素材详解」写作必知篇:语言优美不是作文第一要求