forked from garimasingh128/CP-DSA-Cpp-C
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmeticcompression.cpp
More file actions
102 lines (94 loc) · 1.6 KB
/
arithmeticcompression.cpp
File metadata and controls
102 lines (94 loc) · 1.6 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<bits/stdc++.h>
using namespace std;
struct lowhigh
{
int low;
int high;
};
lowhigh probabilty(char arr[], int n, int cf[],char word, int pro[], int x)
{
struct lowhigh lh;
/**for(int i=0;i<n;i++)
{
if(arr[i]==word[i])
{
lh.high=lh.high+lh.low+pro[i];
}
else
{
lh.low=lh.low+pro[i];
}
}*/
lh.high=cf[x+1]; lh.low=cf[x];
for(int i=0;i<n;i++)
{
pro[i]=pro[i]*pro[x];
}
for(int i=0;i<n+1;i++)
{
cf[i]=cf[i]+pro[i];
}
return lh;
}
void swap(char *xp, char *yp)
{
char temp = *xp;
*xp = *yp;
*yp = temp;
}
// An optimized version of Bubble Sort
void bubbleSort(int arr[], int n, char c[])
{
int i, j;
bool swapped;
for (i = 0; i < n-1; i++)
{
swapped = false;
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
char temp;
temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
swap(&c[j], &c[j+1]);
swapped = true;
}
}
// IF no two elements were swapped by inner loop, then break
if (swapped == false)
break;
}
}
int main()
{
int n;
cout<<"enter n";
cin>>n;
char arr[n];
char word[n];
int pro[n];
for(int i=0;i<n;i++)
{
cout<<"enter letter and its probabailty";
cin>>arr[i]>>pro[i];
}
bubbleSort(pro,n,arr);
int cf[n];
for(int i=0;i<n+1;i++)
{
cf[i]=cf[i]+pro[i];
}
cout<<"enter word";
cin>>word;
struct lowhigh lh;
for(int i=0;i<strlen(word);i++)
{
int x=i;
lh=probabilty(arr,n,cf,word[i],pro,x);
}
double mean=(lh.low+lh.high)/2;
cout<<mean;
return 0;
}