mirror of
https://github.com/InternLM/InternBootcamp.git
synced 2026-04-24 17:05:00 +00:00
198 lines
5.6 KiB
Python
Executable file
198 lines
5.6 KiB
Python
Executable file
"""#
|
|
|
|
### 谜题描述
|
|
Andrew and Eugene are playing a game. Initially, Andrew has string s, consisting of digits. Eugene sends Andrew multiple queries of type \"di → ti\", that means \"replace all digits di in string s with substrings equal to ti\". For example, if s = 123123, then query \"2 → 00\" transforms s to 10031003, and query \"3 → \" (\"replace 3 by an empty string\") transforms it to s = 1212. After all the queries Eugene asks Andrew to find the remainder after division of number with decimal representation equal to s by 1000000007 (109 + 7). When you represent s as a decimal number, please ignore the leading zeroes; also if s is an empty string, then it's assumed that the number equals to zero.
|
|
|
|
Andrew got tired of processing Eugene's requests manually and he asked you to write a program for that. Help him!
|
|
|
|
Input
|
|
|
|
The first line contains string s (1 ≤ |s| ≤ 105), consisting of digits — the string before processing all the requests.
|
|
|
|
The second line contains a single integer n (0 ≤ n ≤ 105) — the number of queries.
|
|
|
|
The next n lines contain the descriptions of the queries. The i-th query is described by string \"di->ti\", where di is exactly one digit (from 0 to 9), ti is a string consisting of digits (ti can be an empty string). The sum of lengths of ti for all queries doesn't exceed 105. The queries are written in the order in which they need to be performed.
|
|
|
|
Output
|
|
|
|
Print a single integer — remainder of division of the resulting number by 1000000007 (109 + 7).
|
|
|
|
Examples
|
|
|
|
Input
|
|
|
|
123123
|
|
1
|
|
2->00
|
|
|
|
|
|
Output
|
|
|
|
10031003
|
|
|
|
|
|
Input
|
|
|
|
123123
|
|
1
|
|
3->
|
|
|
|
|
|
Output
|
|
|
|
1212
|
|
|
|
|
|
Input
|
|
|
|
222
|
|
2
|
|
2->0
|
|
0->7
|
|
|
|
|
|
Output
|
|
|
|
777
|
|
|
|
|
|
Input
|
|
|
|
1000000008
|
|
0
|
|
|
|
|
|
Output
|
|
|
|
1
|
|
|
|
Note
|
|
|
|
Note that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample).
|
|
|
|
Here is a reference code to solve this task. You can use this to help you genereate cases or validate the solution.
|
|
```python
|
|
MOD = 10**9+7
|
|
|
|
s = raw_input()
|
|
n = int(input())
|
|
qs = [['',s]]+[raw_input().split('->') for i in range(n)]
|
|
|
|
ds = {}
|
|
for i in range(10):
|
|
ds[str(i)] = (10,i)
|
|
|
|
for i in range(n,-1,-1):
|
|
out = 0
|
|
mul = 1
|
|
for d in qs[i][1]:
|
|
out = (out * ds[d][0] + ds[d][1]) % MOD
|
|
mul = (mul * ds[d][0]) % MOD
|
|
ds[qs[i][0]] = (mul,out)
|
|
print(ds[''][1])
|
|
```
|
|
|
|
|
|
请完成上述谜题的训练场环境类实现,包括所有必要的方法。
|
|
"""
|
|
|
|
from bootcamp import Basebootcamp
|
|
from bootcamp import Basebootcamp
|
|
import random
|
|
import re
|
|
|
|
MOD = 10**9 + 7
|
|
|
|
class Csubstitutesinnumberbootcamp(Basebootcamp):
|
|
def __init__(self, **params):
|
|
self.params = {
|
|
'min_initial_length': params.get('min_initial_length', 1),
|
|
'max_initial_length': params.get('max_initial_length', 10),
|
|
'max_queries': params.get('max_queries', 5),
|
|
'max_ti_length': params.get('max_ti_length', 5),
|
|
}
|
|
|
|
def case_generator(self):
|
|
s_length = random.randint(self.params['min_initial_length'], self.params['max_initial_length'])
|
|
s = ''.join(random.choices('0123456789', k=s_length))
|
|
n = random.randint(0, self.params['max_queries'])
|
|
queries = []
|
|
for _ in range(n):
|
|
d = random.choice('0123456789')
|
|
ti_length = random.randint(0, self.params['max_ti_length'])
|
|
ti = ''.join(random.choices('0123456789', k=ti_length))
|
|
queries.append(f"{d}->{ti}") # 修改为题目要求的输入格式
|
|
return {
|
|
's': s,
|
|
'queries': queries,
|
|
'n': n
|
|
}
|
|
|
|
@staticmethod
|
|
def prompt_func(question_case) -> str:
|
|
s = question_case['s']
|
|
queries = question_case['queries']
|
|
n = question_case['n']
|
|
query_lines = []
|
|
for idx, query in enumerate(queries, 1):
|
|
d, t = query.split('->')
|
|
t_desc = t if t else "empty string"
|
|
query_lines.append(f"{idx}. Replace '{d}' with '{t_desc}'")
|
|
queries_text = '\n'.join(query_lines) if query_lines else "There are no queries."
|
|
prompt = f"""You are participating in Andrew and Eugene's game. Process the queries and find the remainder.
|
|
|
|
Initial string: {s}
|
|
Number of queries: {n}
|
|
|
|
Queries (in order of application):
|
|
{queries_text}
|
|
|
|
Calculate the remainder after dividing the final number by 1,000,000,007.
|
|
|
|
Rules:
|
|
1. Apply queries in the given order
|
|
2. Leading zeros are NOT removed
|
|
3. Empty string is considered 0
|
|
|
|
Put your final answer between [answer] and [/answer] tags.
|
|
Example: [answer]123[/answer]
|
|
|
|
What is the remainder?"""
|
|
return prompt
|
|
|
|
@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) % MOD
|
|
except ValueError:
|
|
return None
|
|
|
|
@classmethod
|
|
def _verify_correction(cls, solution, identity):
|
|
try:
|
|
# 修复验证逻辑,转换为题目要求的输入格式
|
|
queries = [q.split('->') for q in identity['queries']]
|
|
correct = cls.compute_answer(identity['s'], queries)
|
|
return (solution % MOD) == correct
|
|
except:
|
|
return False
|
|
|
|
@staticmethod
|
|
def compute_answer(s, queries):
|
|
ds = {str(d): [10, d] for d in range(10)}
|
|
|
|
queries = [("", s)] + queries # 添加初始状态
|
|
for i in reversed(range(len(queries))):
|
|
d, t = queries[i]
|
|
out = 0
|
|
mul = 1
|
|
for c in t:
|
|
m, v = ds[c]
|
|
out = (out * m + v) % MOD
|
|
mul = (mul * m) % MOD
|
|
ds[d] = [mul, out]
|
|
return ds[''][1]
|