forked from NJACKWinterOfCode/nwoc_algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_substr.cpp
More file actions
45 lines (40 loc) · 995 Bytes
/
count_substr.cpp
File metadata and controls
45 lines (40 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//Counting Substrings of String Algorithm
//Language Used: C++
//Counting Different Substrings in a given string in N^2 time complexity.
//Input Format: First and only line of input contains input string
//Output Format: Output contains Count of substrings and different substrings
//Sample Input: abbaa
//Sample Output: Number of different substrings is 12
// Different substrings are
// a aa ab abb abba abbaa b ba baa bb bba bbaa
//author:sarthakeddy
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void printsubstring(string s)
{
set<string> str;
ll n=s.length();
for(ll i=0;i<n;i++)
{
string sub="";
for(ll j=i;j<n;j++)
{
sub=sub+s[j];
str.insert(sub);
}
}
cout<<"Number of different substrings is "<<str.size()<<"\n";
cout<<"Different substrings are\n";
for (auto itr : str)
cout<< itr <<" ";
cout<<endl;
}
int main()
{
string s;
cout<<"Enter the string\n";
cin>>s;
printsubstring(s);
return 0;
}