mirror of
https://github.com/InternLM/InternBootcamp.git
synced 2026-04-19 12:58:04 +00:00
init-commit
This commit is contained in:
commit
18a552597a
3461 changed files with 1150579 additions and 0 deletions
209
internbootcamp/bootcamp/ccompressstring/ccompressstring.py
Executable file
209
internbootcamp/bootcamp/ccompressstring/ccompressstring.py
Executable file
|
|
@ -0,0 +1,209 @@
|
|||
"""#
|
||||
|
||||
### 谜题描述
|
||||
Suppose you are given a string s of length n consisting of lowercase English letters. You need to compress it using the smallest possible number of coins.
|
||||
|
||||
To compress the string, you have to represent s as a concatenation of several non-empty strings: s = t_{1} t_{2} … t_{k}. The i-th of these strings should be encoded with one of the two ways:
|
||||
|
||||
* if |t_{i}| = 1, meaning that the current string consists of a single character, you can encode it paying a coins;
|
||||
* if t_{i} is a substring of t_{1} t_{2} … t_{i - 1}, then you can encode it paying b coins.
|
||||
|
||||
|
||||
|
||||
A string x is a substring of a string y if x can be obtained from y by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
|
||||
|
||||
So your task is to calculate the minimum possible number of coins you need to spend in order to compress the given string s.
|
||||
|
||||
Input
|
||||
|
||||
The first line contains three positive integers, separated by spaces: n, a and b (1 ≤ n, a, b ≤ 5000) — the length of the string, the cost to compress a one-character string and the cost to compress a string that appeared before.
|
||||
|
||||
The second line contains a single string s, consisting of n lowercase English letters.
|
||||
|
||||
Output
|
||||
|
||||
Output a single integer — the smallest possible number of coins you need to spend to compress s.
|
||||
|
||||
Examples
|
||||
|
||||
Input
|
||||
|
||||
|
||||
3 3 1
|
||||
aba
|
||||
|
||||
|
||||
Output
|
||||
|
||||
|
||||
7
|
||||
|
||||
|
||||
Input
|
||||
|
||||
|
||||
4 1 1
|
||||
abcd
|
||||
|
||||
|
||||
Output
|
||||
|
||||
|
||||
4
|
||||
|
||||
|
||||
Input
|
||||
|
||||
|
||||
4 10 1
|
||||
aaaa
|
||||
|
||||
|
||||
Output
|
||||
|
||||
|
||||
12
|
||||
|
||||
Note
|
||||
|
||||
In the first sample case, you can set t_{1} = 'a', t_{2} = 'b', t_{3} = 'a' and pay 3 + 3 + 1 = 7 coins, since t_{3} is a substring of t_{1}t_{2}.
|
||||
|
||||
In the second sample, you just need to compress every character by itself.
|
||||
|
||||
In the third sample, you set t_{1} = t_{2} = 'a', t_{3} = 'aa' and pay 10 + 1 + 1 = 12 coins, since t_{2} is a substring of t_{1} and t_{3} is a substring of t_{1} t_{2}.
|
||||
|
||||
Here is a reference code to solve this task. You can use this to help you genereate cases or validate the solution.
|
||||
```python
|
||||
import sys
|
||||
range = xrange
|
||||
input = raw_input
|
||||
|
||||
n,A,B = [int(x) for x in input().split()]
|
||||
s = input()
|
||||
|
||||
DP = [0]*(n+1)
|
||||
for i in range(n):
|
||||
a = 0
|
||||
b = i+1
|
||||
while a!=b:
|
||||
c = (a+b)//2
|
||||
if s[c:i+1] in s[:c]:
|
||||
b = c
|
||||
else:
|
||||
a = c+1
|
||||
|
||||
if a==i+1:
|
||||
score = 10**9
|
||||
else:
|
||||
score = DP[a]+B
|
||||
|
||||
score = min(score,DP[i]+A)
|
||||
DP[i+1] = score
|
||||
print DP[n]
|
||||
```
|
||||
|
||||
|
||||
请完成上述谜题的训练场环境类实现,包括所有必要的方法。
|
||||
"""
|
||||
|
||||
from bootcamp import Basebootcamp
|
||||
import random
|
||||
import string
|
||||
import re
|
||||
|
||||
from bootcamp import Basebootcamp
|
||||
|
||||
def compute_min_coins(n, a_val, b_val, s):
|
||||
DP = [0] * (n + 1)
|
||||
for i in range(n):
|
||||
low = 0
|
||||
high = i + 1
|
||||
while low < high:
|
||||
mid = (low + high) // 2
|
||||
substring = s[mid:i+1]
|
||||
if substring in s[:mid]:
|
||||
high = mid
|
||||
else:
|
||||
low = mid + 1
|
||||
best_c = low
|
||||
if best_c == i + 1:
|
||||
cost_with_b = float('inf')
|
||||
else:
|
||||
cost_with_b = DP[best_c] + b_val
|
||||
cost_single = DP[i] + a_val
|
||||
DP[i+1] = min(cost_with_b, cost_single)
|
||||
return DP[n]
|
||||
|
||||
class Ccompressstringbootcamp(Basebootcamp):
|
||||
def __init__(self, n_min=1, n_max=5000, a_min=1, a_max=5000, b_min=1, b_max=5000):
|
||||
self.n_min = n_min
|
||||
self.n_max = n_max
|
||||
self.a_min = a_min
|
||||
self.a_max = a_max
|
||||
self.b_min = b_min
|
||||
self.b_max = b_max
|
||||
|
||||
def case_generator(self):
|
||||
n = random.randint(self.n_min, self.n_max)
|
||||
a = random.randint(self.a_min, self.a_max)
|
||||
b = random.randint(self.b_min, self.b_max)
|
||||
s = ''.join(random.choices(string.ascii_lowercase, k=n))
|
||||
correct_answer = compute_min_coins(n, a, b, s)
|
||||
return {
|
||||
'n': n,
|
||||
'a': a,
|
||||
'b': b,
|
||||
's': s,
|
||||
'correct_answer': correct_answer
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def prompt_func(question_case) -> str:
|
||||
n = question_case['n']
|
||||
a = question_case['a']
|
||||
b = question_case['b']
|
||||
s = question_case['s']
|
||||
problem_description = f"""You are a string compression expert. Your task is to find the minimum number of coins required to compress a given string according to specific rules.
|
||||
|
||||
The rules for compression are as follows:
|
||||
1. You split the string into one or more non-empty substrings t₁, t₂, ..., t_k.
|
||||
2. For each substring t_i:
|
||||
- If the length of t_i is 1 (single character), it costs {a} coins.
|
||||
- If t_i is a substring of the concatenation of all previous substrings (t₁t₂…t_{{i-1}}), it costs {b} coins instead.
|
||||
- Note: A substring can be formed by deleting zero or more characters from the start and/or end of the original string. For example, "abc" has substrings like "a", "ab", "bc", "abc", etc.
|
||||
|
||||
You are given:
|
||||
- The first line contains three integers: {n} {a} {b} (the string's length, cost for single-character substrings, cost for existing substrings).
|
||||
- The second line contains the string: {s}
|
||||
|
||||
Your goal is to compute the minimum total coins required. Output a single integer within [answer] and [/answer] tags.
|
||||
|
||||
Example:
|
||||
Input:
|
||||
3 3 1
|
||||
aba
|
||||
Output:
|
||||
7 (split as 'a', 'b', 'a' costing 3 + 3 + 1 = 7 coins)
|
||||
|
||||
Now, solve the following problem:
|
||||
Input:
|
||||
{n} {a} {b}
|
||||
{s}
|
||||
|
||||
Please provide your answer as a single integer enclosed within [answer] and [/answer]."""
|
||||
return problem_description
|
||||
|
||||
@staticmethod
|
||||
def extract_output(output):
|
||||
matches = re.findall(r'\[answer\](.*?)\[/answer\]', output, re.DOTALL)
|
||||
if not matches:
|
||||
return None
|
||||
last_match = matches[-1].strip()
|
||||
try:
|
||||
return int(last_match)
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def _verify_correction(cls, solution, identity):
|
||||
return solution == identity['correct_answer']
|
||||
Loading…
Add table
Add a link
Reference in a new issue