forked from shikharkohli/c---files
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithcoding.cpp.u1conflict.1
More file actions
executable file
·55 lines (51 loc) · 975 Bytes
/
arithcoding.cpp.u1conflict.1
File metadata and controls
executable file
·55 lines (51 loc) · 975 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
46
47
48
49
50
51
52
53
54
55
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
struct chars
{
char ch;
double p;
};
bool compare(chars i,chars j)
{
if(i.p>j.p)
return true;
else
return false;
}
using namespace std;
int main()
{
string input;
getline(cin,input,'\n');
map<char,int> charp;
for(int i=0;i<input.length();i++)
charp[input[i]]++;
vector<chars> plist;
for(map<char,int>::iterator i=charp.begin();i!=charp.end();i++)
{
chars temp;
temp.ch=i->first;
temp.p=(double)i->second/input.length();
plist.push_back(temp);
}
//cout<<i->first<<","<<i->second<<endl;
sort(plist.begin(),plist.end(),compare);
for(int i=0;i<plist.size();i++)
{
cout<<plist[i].ch<<" "<<plist[i].p<<endl;
}
double ub=1.0,lb=0.0;
for(int i=0;i<input.length();i++)
{
for(int k=0;k<plist.size() && plist[k].ch!=input[i];k++)
{
ub+=(ub-lb)*plist[i].p;
}
lb=ub;
ub+=(ub-lb)*plist[i].p;
}
cout<<"Lower bound "<<lb<<" Upper bound "<<ub<<endl;
return 0;
}