- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Find the Longest Prefix Matching of a Given Sequence
Here we shall discuss a C++ program to find the Longest Subsequence Common to All Sequences in a Set of Sequences.
Algorithms
Begin Take the array of strings as input. function matchedPrefixtill(): find the matched prefix between string s1 and s2 : n1 = store length of string s1. n2 = store length of string s2. for i = 0, j = 0 to i <= n1 – 1 && j <= n2 - 1 if s1[i] != s2[j] break result.push_back(s1[i]) return result End Begin function matchedPrefix(): returns the longest matched prefix from the array of strings: for int i = 1 to n - 1 pre = matchedPrefixtill(pre, a[i]) return pre. End
Example
#include<bits/stdc++.h> using namespace std; string matchedPrefixtill(string s1, string s2) { string res; int n1 = s1.length(); //store length of string s1. int n2 = s2.length(); //store length of string s2. for (int i = 0, j = 0; i <= n1 - 1 && j <= n2 - 1; i++, j++) { if (s1[i] != s2[j]) break; res.push_back(s1[i]); } return (res); } string matchedPrefix (string a[], int n) { string pre = a[0]; for (int i = 1; i <= n - 1; i++) pre = matchedPrefixtill(pre, a[i]); return (pre); } int main() { string a[] = {"Tutorialspoint", "Tutor", "Tutorials"}; //taking inputs int n = sizeof(a) / sizeof(a[0]); string res = matchedPrefix(a, n); if (res.length()) cout<<"Longest common subsequence is matched - "<<res.c_str(); else cout<<"No matched prefix"; return (0); }
Output
Longest common subsequence is matched - Tutor
- Related Articles
- C++ Program to Find the Longest Increasing Subsequence of a Given Sequence
- Program to find length longest prefix sequence of a word array in Python
- Program to find longest prefix that is also a suffix in C++
- Program to find sum of given sequence in C++
- Program to find longest common prefix from list of strings in Python
- Program to find length of longest consecutive sequence in Python
- Print the longest prefix of the given string which is also the suffix of the same string in C Program.
- C++ Program to find out the distinct elements in a given sequence
- Longest Happy Prefix in C++
- Find minimum shift for longest common prefix in C++
- C# Program to find the sum of a sequence
- C++ Program to get length of longest subsequence in n times copied sequence
- Program to find last digit of the given sequence for given n in Python
- C# program to find the length of the Longest Consecutive 1’s in Binary Representation of a given integer
- Longest Arithmetic Sequence in C++

Advertisements