upload
This commit is contained in:
commit
810a9654a4
50 changed files with 4450 additions and 0 deletions
40
7.1/A.cpp
Normal file
40
7.1/A.cpp
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
typedef long long ll;
|
||||
|
||||
void GrayCode(ll n) {
|
||||
if (n <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<std::string> code;
|
||||
|
||||
code.push_back("0");
|
||||
code.push_back("1");
|
||||
|
||||
ll i, j;
|
||||
|
||||
for (i = 2; i < (1 << n); i = i << 1) {
|
||||
for (j = i - 1; j >= 0; j--) {
|
||||
code.push_back(code[j]);
|
||||
}
|
||||
for (j = 0; j < i; j++) {
|
||||
code[j] = "0" + code[j];
|
||||
}
|
||||
for (j = i; j < 2 * i; j++) {
|
||||
code[j] = "1" + code[j];
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < code.size(); i++)
|
||||
std::cout << code[i] << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
ll n;
|
||||
std::cin >> n;
|
||||
GrayCode(n);
|
||||
return 0;
|
||||
}
|
30
7.1/B.py
Normal file
30
7.1/B.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
def lz78(string: str) -> None:
|
||||
buffer = ''
|
||||
enc_dict: dict[str, int] = {}
|
||||
for char in string:
|
||||
comb = buffer + char
|
||||
if comb in enc_dict:
|
||||
buffer += char
|
||||
else:
|
||||
if buffer in enc_dict:
|
||||
print(enc_dict[buffer], char)
|
||||
else:
|
||||
print(0, char)
|
||||
enc_dict[comb] = len(enc_dict) + 1
|
||||
buffer = ""
|
||||
|
||||
if buffer:
|
||||
if buffer in enc_dict:
|
||||
print(str(enc_dict[buffer]) + ' \0')
|
||||
else:
|
||||
print(enc_dict[buffer[:-1]], buffer[-1])
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
s = input()
|
||||
lz78(s)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
58
7.1/E.py
Normal file
58
7.1/E.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
import dataclasses
|
||||
|
||||
@dataclasses.dataclass
|
||||
class Segment:
|
||||
l: float
|
||||
r: float
|
||||
|
||||
def find_alphabet(s: str) -> list[str]:
|
||||
alphabet = []
|
||||
for x in s:
|
||||
if x not in alphabet:
|
||||
alphabet.append(x)
|
||||
return alphabet
|
||||
|
||||
def find_probabilities(s: str, alphabet: list[str]) -> dict[str, Segment]:
|
||||
count: dict[str, int] = {}
|
||||
for x in s:
|
||||
if x not in count:
|
||||
count[x] = 0
|
||||
count[x] += 1
|
||||
|
||||
left = 0.0
|
||||
slice = 1.0 / len(s)
|
||||
segments: dict[str, Segment] = {}
|
||||
|
||||
for x in sorted(alphabet):
|
||||
segments[x] = Segment(left,left + (slice * count[x]))
|
||||
left += slice * count[x]
|
||||
|
||||
return segments
|
||||
|
||||
|
||||
def arithmetic_coding(s: str) -> float:
|
||||
alphabet = find_alphabet(s)
|
||||
probabilities = find_probabilities(s, alphabet)
|
||||
|
||||
left = 0.0
|
||||
right = 1.0
|
||||
|
||||
for x in s:
|
||||
new_left = left + (right - left) * probabilities[x].l
|
||||
new_right = left + (right - left) * probabilities[x].r
|
||||
|
||||
left = new_left
|
||||
right = new_right
|
||||
|
||||
return left
|
||||
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
s = input()
|
||||
print(round(arithmetic_coding(s), 6))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue