字符串数组中最长的公共子串
我们得到了一个共享一个共同词干的词列表,即这些词来自同一个词,例如:词悲伤、悲伤和悲伤都来自词干'sad' 。
我们的任务是找到并返回最长的公共子串,也称为这些词的词干。如果有平局,我们会按字母顺序选择最小的一个。
例子:
Input : grace graceful disgraceful gracefully
Output : grace
Input : sadness sad sadly
Output : sad
这个想法是将列表中的任何单词作为参考,并形成其所有子字符串并遍历整个列表,检查生成的子字符串是否出现在所有子字符串中。
下面是上述思想的实现:
C++
// C++ program to find the stem of given list of
// words
#include
using namespace std;
// function to find the stem (longest common
// substring) from the string array
string findstem(vector arr)
{
// Determine size of the array
int n = arr.size();
// Take first word from array as reference
string s = arr[0];
int len = s.length();
string res = "";
for (int i = 0; i < len; i++) {
for (int j = i + 1; j <= len; j++) {
// generating all possible substrings
// of our reference string arr[0] i.e s
string stem = s.substr(i, j);
int k = 1;
for (k = 1; k < n; k++) {
// Check if the generated stem is
// common to all words
if (arr[k].find(stem) == std::string::npos)
break;
}
// If current substring is present in
// all strings and its length is greater
// than current result
if (k == n && res.length() < stem.length())
res = stem;
}
}
return res;
}
// Driver code
int main()
{
vector arr{ "grace", "graceful", "disgraceful",
"gracefully" };
// Function call
string stems = findstem(arr);
cout << stems << endl;
}
// This code is contributed by
// sanjeev2552
Java
// Java program to find the stem of given list of
// words
import java.io.*;
import java.util.*;
class stem {
// function to find the stem (longest common
// substring) from the string array
public static String findstem(String arr[])
{
// Determine size of the array
int n = arr.length;
// Take first word from array as reference
String s = arr[0];
int len = s.length();
String res = "";
for (int i = 0; i < len; i++) {
for (int j = i + 1; j <= len; j++) {
// generating all possible substrings
// of our reference string arr[0] i.e s
String stem = s.substring(i, j);
int k = 1;
for (k = 1; k < n; k++)
// Check if the generated stem is
// common to all words
if (!arr[k].contains(stem))
break;
// If current substring is present in
// all strings and its length is greater
// than current result
if (k == n && res.length() < stem.length())
res = stem;
}
}
return res;
}
// Driver Code
public static void main(String args[])
{
String arr[] = { "grace", "graceful",
"disgraceful","gracefully" };
// Function call
String stems = findstem(arr);
System.out.println(stems);
}
}
Python 3
# Python 3 program to find the stem
# of given list of words
# function to find the stem (longest
# common substring) from the string array
def findstem(arr):
# Determine size of the array
n = len(arr)
# Take first word from array
# as reference
s = arr[0]
l = len(s)
res = ""
for i in range(l):
for j in range(i + 1, l + 1):
# generating all possible substrings
# of our reference string arr[0] i.e s
stem = s[i:j]
k = 1
for k in range(1, n):
# Check if the generated stem is
# common to all words
if stem not in arr[k]:
break
# If current substring is present in
# all strings and its length is greater
# than current result
if (k + 1 == n and len(res) < len(stem)):
res = stem
return res
# Driver Code
if __name__ == "__main__":
arr = ["grace", "graceful",
"disgraceful", "gracefully"]
# Function call
stems = findstem(arr)
print(stems)
# This code is contributed by ita_c
C#
// C# program to find the stem of given list of
// words
using System;
using System.Collections.Generic;
class stem
{
// function to find the stem (longest common
// substring) from the string array
public static String findstem(String []arr)
{
// Determine size of the array
int n = arr.Length;
// Take first word from array as reference
String s = arr[0];
int len = s.Length;
String res = "";
for (int i = 0; i < len; i++)
{
for (int j = i + 1; j <= len; j++)
{
// generating all possible substrings
// of our reference string arr[0] i.e s
String stem = s.Substring(i, j-i);
int k = 1;
for (k = 1; k < n; k++)
// Check if the generated stem is
// common to all words
if (!arr[k].Contains(stem))
break;
// If current substring is present in
// all strings and its length is greater
// than current result
if (k == n && res.Length < stem.Length)
res = stem;
}
}
return res;
}
// Driver Code
public static void Main(String []args)
{
String []arr = { "grace", "graceful", "disgraceful",
"gracefully" };
// Function call
String stems = findstem(arr);
Console.WriteLine(stems);
}
}
// This code contributed by Rajput-Ji
PHP
输出
grace