이 문제에서는 문자열이 주어지고 주어진 문자열에서 부분 문자열을 찾아야 합니다. 찾을 하위 문자열은 모음으로 시작하고 상수 문자로 끝나야 합니다.
문자열 문자 배열입니다.
이 문제에서 생성될 부분 문자열은 문자열의 일부 문자를 삭제하여 생성할 수 있습니다. 그리고 문자열의 순서를 바꾸지 않고.
Input: ‘abc’ Output: ab, ac, abc
이 문제를 해결하기 위해 문자열을 반복하고 모음을 수정하고 다음 시퀀스를 확인합니다. 솔루션을 찾는 알고리즘을 살펴보겠습니다 −
알고리즘
Step 1: Iterate of each character of the string, with variable i. Step 2: If the ith character is a vowel. Step 3: If the jth character is a consonant. Step 4: Add to the HashSet, substring from 1st character to jth character. Step 5: Repeat the following steps and find substrings from the string.
반복 접근 방식에서는 모든 문자열을 반복합니다. 1에서 2까지 다리(문자열) -1.
예시
#include <bits/stdc++.h>
using namespace std;
string subString(string s, int binary){
string sub = "";
int pos;
while(binary>0){
pos=log2(binary&-binary)+1;
sub=s[pos-1]+sub;
binary= (binary & ~(1 << (pos-1)));
}
reverse(sub.begin(),sub.end());
return sub;
}
void findAllSubStrings(string s){
map<int, set<string> > sorted_subsequence;
int len = s.size();
int limit = pow(2, len);
for (int i = 1; i <= limit - 1; i++) {
string sub = subString(s, i);
sorted_subsequence[sub.length()].insert(sub);
}
for (auto it : sorted_subsequence) {
for (auto ii : it.second)
cout<<ii<<" ";
cout<<"\t";
}
}
int main() {
string s = "wxyz";
cout<<"The substring are :\n";
findAllSubStrings(s);
return 0;
} 출력
부분 문자열은 -
w x y z wx wy wz xy xz yz wxy wxz wyz xyz wxyz