Skip to content

Commit df0f8f9

Browse files
committed
Q4_all data type map completed
1 parent 44d3d96 commit df0f8f9

File tree

5 files changed

+459
-0
lines changed

5 files changed

+459
-0
lines changed

APS Assignment 2.pdf

93.6 KB
Binary file not shown.

Q1a_2018201033.cpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int *finalsuffixans;
4+
struct suffixArray{
5+
int index;
6+
int tuple[2];
7+
};
8+
9+
bool comfun(struct suffixArray obj1, struct suffixArray obj2)
10+
{
11+
if(obj1.tuple[0]==obj2.tuple[0])
12+
{
13+
if(obj1.tuple[1]<obj2.tuple[1])
14+
return true;
15+
else
16+
return false;
17+
}
18+
else if(obj1.tuple[0]<obj2.tuple[0])
19+
{
20+
return true;
21+
}
22+
else
23+
{
24+
return false;
25+
}
26+
}
27+
void createsufficArray(string str)
28+
{
29+
int size1=str.length();
30+
struct suffixArray suffobj[size1];
31+
int suffInd[size1];
32+
33+
// Intitialisation of tuple w.r.t first two character
34+
for(int i=0;i<size1;i++)
35+
{
36+
suffobj[i].index=i;
37+
suffobj[i].tuple[0]=str[i]-'0';
38+
if(i<size1-1)
39+
{
40+
suffobj[i].tuple[1]=str[i+1]-'0';
41+
}
42+
else{
43+
suffobj[i].tuple[1]=-1;
44+
}
45+
46+
}
47+
48+
//sort according to tuple;
49+
sort(suffobj,suffobj+size1, comfun);
50+
51+
for(int j=4;j<(2*size1);j=j*2)
52+
{
53+
int curRank=0;
54+
int preRank=suffobj[0].tuple[0];
55+
suffobj[0].tuple[0]=curRank;
56+
suffInd[suffobj[0].index]=0;
57+
58+
for(int k=1;k<size1;k++)
59+
{
60+
int curtuple1=suffobj[k].tuple[0];
61+
int curtuple2=suffobj[k].tuple[1];
62+
int pretuple2=suffobj[k-1].tuple[1];
63+
if(curtuple1==preRank && curtuple2==pretuple2)
64+
{
65+
preRank=suffobj[k].tuple[0];
66+
suffobj[k].tuple[0]= curRank;
67+
}
68+
else{
69+
preRank=suffobj[k].tuple[0];
70+
suffobj[k].tuple[0]=++curRank;
71+
}
72+
suffInd[suffobj[k].index]=k;
73+
}
74+
75+
for (int i=0;i<size1;i++)
76+
{
77+
int nextind = suffobj[i].index + j/2;
78+
if(nextind < size1){
79+
suffobj[i].tuple[1]=suffobj[suffInd[nextind]].tuple[0];
80+
}
81+
else{
82+
suffobj[i].tuple[1]=-1;
83+
}
84+
}
85+
86+
sort(suffobj, suffobj+size1, comfun);
87+
88+
}
89+
90+
// Store indexes of all sorted suffixes in the suffix array
91+
finalsuffixans = new int[size1];
92+
for (int i=0;i<size1;i++)
93+
{
94+
finalsuffixans[i] = suffobj[i].index;
95+
}
96+
97+
}
98+
99+
int main()
100+
{
101+
string s;
102+
cin>>s;
103+
createsufficArray(s);
104+
cout<<"Suffix Array for given string : ";
105+
int len=s.length();
106+
for(int i=0;i<len;i++)
107+
{
108+
cout<< finalsuffixans[i]<<" ";
109+
}
110+
int small=finalsuffixans[0];
111+
string nextsub=s.substr(small);
112+
//cout<<nextsub<<endl;
113+
string presub=s.substr(0,small);
114+
//cout<<presub<<endl;
115+
string lexsmallstr=nextsub+presub;
116+
117+
cout<<"\nMinimum Lexicographic rotation : "<<lexsmallstr<<endl;
118+
119+
return 0;
120+
121+
}

Q4_2018201033.cpp

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define SIZE 3
4+
#define X 31
5+
6+
template <class T, class U>
7+
class Unordered_map
8+
{
9+
class node
10+
{
11+
public:
12+
T kunique;
13+
U val;
14+
node *next;
15+
};
16+
17+
node *hashtable[SIZE];
18+
19+
public:
20+
//Constructor
21+
Unordered_map()
22+
{
23+
for (int i = 0; i < SIZE; i++)
24+
{
25+
hashtable[i] = NULL;
26+
}
27+
}
28+
29+
void printall()
30+
{
31+
cout << "\n*********************" << endl;
32+
for (int i = 0; i < SIZE; i++)
33+
{
34+
cout << hashtable[i] << " ";
35+
if (hashtable[i] != NULL)
36+
{
37+
node *cur = hashtable[i];
38+
while (cur != NULL)
39+
{
40+
cout << cur->kunique << " : ";
41+
cout << cur->val << " || ";
42+
cur = cur->next;
43+
}
44+
}
45+
cout << endl;
46+
}
47+
cout << "\n*********************" << endl;
48+
}
49+
50+
node *getnewNode(T key1, U value1)
51+
{
52+
node *new_node = new node;
53+
new_node->kunique = key1;
54+
new_node->val = value1;
55+
new_node->next = NULL;
56+
//cout << "\nNode created successfully";
57+
return new_node;
58+
}
59+
60+
long long getHashIndex(T key)
61+
{
62+
ostringstream str1;
63+
str1 << key;
64+
string str = str1.str();
65+
//cout<<str<<endl;
66+
long long result = str[0];
67+
for (long long i = 1; i < str.length(); i++)
68+
{
69+
result = result * X + str[i];
70+
}
71+
long long index = result % SIZE;
72+
return index;
73+
}
74+
75+
void insertintohash(T key, U value)
76+
{
77+
printall();
78+
long long index = getHashIndex(key);
79+
cout << "Hash Index : " << index << endl;
80+
if (hashtable[index] == NULL)
81+
{
82+
hashtable[index] = getnewNode(key, value);
83+
cout << "Node inserted Succesfully" << endl;
84+
}
85+
else
86+
{
87+
node *cur = hashtable[index];
88+
int flag = 0;
89+
while (cur->next != NULL)
90+
{
91+
if (cur->kunique == key)
92+
{
93+
flag = 1;
94+
break;
95+
}
96+
cur = cur->next;
97+
}
98+
if (!flag && cur->kunique!=key)
99+
{
100+
cur->next = getnewNode(key, value);
101+
cout << "Node inserted Succesfully" << endl;
102+
}
103+
else
104+
{
105+
cout << "Key Already present in map" << endl;
106+
}
107+
}
108+
printall();
109+
}
110+
U searchvalue(T key)
111+
{
112+
long long index = getHashIndex(key);
113+
//cout << "Hash Index : " << index << endl;
114+
if (hashtable[index] == NULL)
115+
return "-1";
116+
else
117+
{
118+
node *cur = hashtable[index];
119+
while (cur != NULL && cur->kunique != key)
120+
{
121+
cur = cur->next;
122+
}
123+
if (cur == NULL)
124+
return "-1";
125+
else
126+
{
127+
return cur->val;
128+
}
129+
}
130+
}
131+
void deletekey(T key)
132+
{
133+
printall();
134+
long long index = getHashIndex(key);
135+
cout<<"in delete Hash Index : "<<index<<endl;
136+
if (hashtable[index] == NULL)
137+
{
138+
printf("\nKey not found in Map");
139+
return;
140+
}
141+
else
142+
{
143+
node *head = hashtable[index];
144+
node *tra = head;
145+
if (head->kunique == key)
146+
{
147+
tra = head;
148+
head = head->next;
149+
free(tra);
150+
hashtable[index] = head;
151+
printall();
152+
return;
153+
}
154+
else
155+
{
156+
node *pre;
157+
while (tra->next != NULL && tra->kunique != key)
158+
{
159+
pre = tra;
160+
tra = tra->next;
161+
}
162+
if (tra->next == NULL && tra->kunique != key)
163+
{
164+
printf("\nKey not found in Map");
165+
return;
166+
}
167+
else //tra->data==ele
168+
{
169+
pre->next = tra->next;
170+
free(tra);
171+
}
172+
printall();
173+
}
174+
}
175+
}
176+
};
177+
int main()
178+
{
179+
string key;
180+
string value;
181+
Unordered_map<string, string> mp;
182+
int ch;
183+
do
184+
{
185+
cout << "\n1.insert";
186+
cout << "\n2.search";
187+
cout << "\n3.Delete";
188+
cout << "\nEnter You choice : ";
189+
cin >> ch;
190+
if (ch == 1)
191+
{
192+
cin >> key;
193+
cin >> value;
194+
mp.insertintohash(key, value);
195+
}
196+
else if (ch == 2)
197+
{
198+
cin >> key;
199+
cout << "Value : " << mp.searchvalue(key);
200+
}
201+
else if (ch == 3)
202+
{
203+
cin >> key;
204+
mp.deletekey(key);
205+
}
206+
else
207+
{
208+
cout << "Invalid Argument";
209+
}
210+
211+
} while (ch != 0);
212+
213+
return 0;
214+
}

0 commit comments

Comments
 (0)