#include<bits/stdc++.h>
using namespace std;
//Problem 1 : Number of distinct substrings
int solve_p1(const string &str){
unordered_set<string>s;
int cnt =0;
for(int i =0;i<str.size();i++){
for(int j=i+1;j<=str.size();j++){
string sub = str.substr(i, j - i);
if(s.count(sub)){
continue;
}
s.insert(sub);
cnt++;
}
}
return cnt;
}
//Problem 2 : common substrings
void solve_p2(string s1,string s2){
unordered_set<string>set1;
unordered_set<string>set2;
int cnt =0;
for(int i =0;i<s1.size();i++){
for(int j=i+1;j<=s1.size();j++){
string sub = s1.substr(i, j - i);
set1.insert(sub);
}
}
for(int i =0;i<s2.size();i++){
for(int j=i+1;j<=s2.size();j++){
string sub = s2.substr(i, j - i);
set2.insert(sub);
}
}
for(auto s : set1){
if(set2.count(s)){
cnt++;
cout << s << " ";
}
}
cout <<"cnt : "<<cnt <<'\n';
}
//Problem 3 : cnt unique anagrams
int solve_p3(const string &str){
set<string>st;
int cnt = 0;
for(int i = 0; i < str.size(); i++){
for(int j = i + 1; j <= str.size(); j++){
string sub = str.substr(i, j - i);
sort(sub.begin(),sub.end());
if (!st.count(sub)) {
st.insert(sub);
cnt++;
}
}
}
return cnt;
}
//Problem 4:
class Node{
public:
int data;
Node* next;
Node(int data){
this->data =data;
this->next =nullptr;
}
};
class LinkedList{
private:
Node* head;
Node* tail;
int size;
public:
LinkedList() : head(nullptr), tail(nullptr), size(0) {}
void print() {
unordered_set<Node*> visited;
Node* cur = head;
while (cur) {
if (visited.find(cur) != visited.end()) {
cout << " -> [Cycle detected at " << cur->data << "]\n";
return;
}
cout << cur->data << " ";
visited.insert(cur);
cur = cur->next;
}
cout << "\n";
}
void insert_end(int value) {
Node* newNode = new Node(value);
if (!head) {
head = tail = newNode;
}
else {
tail->next = newNode;
tail = newNode;
}
size++;
}
void create_cycle(){
for(int i =0;i<4;i++){
insert_end(i);
}
Node* now = tail;
for(int i =0;i<3;i++){
insert_end(14+i);
}
tail->next = now;
}
void remove_cycle(){
unordered_set<Node*> visited;
Node* cur = head;
while(cur!=nullptr && cur->next !=nullptr){
if(visited.find(cur->next) !=visited.end()){
cur->next =nullptr;
cout <<"Cycle found and removed\n";
return;
}
visited.insert(cur);
cur = cur->next;
}
cout <<"No cycle found\n";
}
};
//Problem 5 :Quadratic Probing
int hash_string(string name , int limit){
long long sum=0, nn =limit;
for(int i=0;i<(int)name.size();i++){
sum = (sum * 26 + name[i] - 'a') % nn;
}
return sum % nn;
}
class Entry{
public:
const static int Internal_limit=65407;
string name;//key
string phoneNumber;//data
Entry(string name,string phoneNumber):
name(name),phoneNumber(phoneNumber){ }
int hash(){
return hash_string(name,Internal_limit);
}
void print(){
cout <<setw(2)<< " ("<<name <<", "<<phoneNumber<<") "<<'\n';
}
};
class HashTable{
private:
int table_size{0};
vector<Entry*>table;
Entry* deleted = new Entry(""," ");
public:
HashTable(int table_size):table_size(table_size){
table.resize(table_size);
}
bool put(Entry phone){
int idx = phone.hash() % table_size;
for(int i = 0; i < table_size; i++){
int probe_idx = (idx + i*i) % table_size;
if(table[probe_idx]==deleted || !table[probe_idx]){
table[probe_idx] = new Entry(phone.name, phone.phoneNumber);
return true;
}
else if(table[probe_idx]->name == phone.name){
table[probe_idx]->phoneNumber = phone.phoneNumber;
return true;
}
}
return false;
}
void rehash(){
int old_size = table_size;
table_size *= 2;
vector<Entry*> old_table = table;
table.clear();
table.resize(table_size);
for(auto en : old_table){
if(en && en != deleted){
put(*en);
}
}
}
void print_all(){
for(int i = 0 ; i < table_size ; i++){
cout << i <<" ";
if(table[i]==deleted){
cout <<" X ";
}
else if(!table[i]){
cout<<" E ";
}
else{
table[i]->print();
}
cout <<'\n';
}
cout <<"*************************\n";
}
};
int main(){
cout <<"Test problem 1 : \n";
cout << solve_p1("abcdef") << endl;
cout <<endl<<endl;
cout <<"Test problem 2 : \n";
solve_p2("aaab","ab");
cout <<endl<<endl;
cout <<"Test problem 3 : \n";
cout << solve_p3("aab")<<endl;
cout <<endl<<endl;
cout <<"Test problem 4 : \n";
LinkedList list;
list.create_cycle();
list.remove_cycle();
list.print();
cout <<endl << endl;
cout <<"Test problem 5 : \n";
HashTable ht(7);
ht.put(Entry("alice", "12345"));
ht.put(Entry("bob", "67890"));
ht.put(Entry("charlie", "54321"));
ht.print_all();
cout <<"Rehashing...\n";
ht.rehash();
ht.print_all();
return 0;
}