| import random | |
| import string | |
| def generate_random_string(length): | |
| return ''.join(random.choice(string.ascii_lowercase) for _ in range(length)) | |
| def generate_operation(current_length, max_queries_left): | |
| operations = ['Q', 'R', 'I'] | |
| # Bias towards queries if we have many left to generate | |
| if max_queries_left > 0 and random.random() < 0.4: | |
| op = 'Q' | |
| else: | |
| op = random.choice(operations) | |
| if op == 'Q': | |
| x = random.randint(1, current_length) | |
| y = random.randint(1, current_length) | |
| return f"Q {x} {y}", current_length, 1 | |
| elif op == 'R': | |
| x = random.randint(1, current_length) | |
| d = random.choice(string.ascii_lowercase) | |
| return f"R {x} {d}", current_length, 0 | |
| else: # op == 'I' | |
| x = random.randint(0, current_length) | |
| d = random.choice(string.ascii_lowercase) | |
| return f"I {x} {d}", current_length + 1, 0 | |
| def generate_test_case(): | |
| # Generate initial string | |
| initial_length = random.randint(1, min(100, random.choice([10, 50, 100]))) | |
| initial_string = generate_random_string(initial_length) | |
| # Generate number of operations | |
| m = random.randint(1, min(150000, random.choice([50, 500, 5000, 50000]))) | |
| operations = [] | |
| current_length = initial_length | |
| queries_generated = 0 | |
| max_queries = min(10000, m // 2) # Ensure we don't exceed query limit | |
| for _ in range(m): | |
| if current_length >= 100000: # Prevent string from getting too long | |
| # Only allow queries and replacements | |
| if queries_generated < max_queries and random.random() < 0.7: | |
| x = random.randint(1, current_length) | |
| y = random.randint(1, current_length) | |
| operations.append(f"Q {x} {y}") | |
| queries_generated += 1 | |
| else: | |
| x = random.randint(1, current_length) | |
| d = random.choice(string.ascii_lowercase) | |
| operations.append(f"R {x} {d}") | |
| else: | |
| queries_left = max_queries - queries_generated | |
| op, current_length, query_count = generate_operation(current_length, queries_left) | |
| operations.append(op) | |
| queries_generated += query_count | |
| # Build the input string | |
| result = initial_string + '\n' | |
| result += str(len(operations)) + '\n' | |
| result += '\n'.join(operations) | |
| return result | |
| def construct_inputs(): | |
| inputs = [] | |
| # Small test cases | |
| for _ in range(10): | |
| inputs.append(generate_test_case()) | |
| # Medium test cases | |
| for _ in range(10): | |
| inputs.append(generate_test_case()) | |
| # Large test cases | |
| for _ in range(10): | |
| inputs.append(generate_test_case()) | |
| # Edge cases | |
| # Single character string | |
| edge1 = "a\n3\nQ 1 1\nR 1 b\nQ 1 1" | |
| inputs.append(edge1) | |
| # String with all same characters | |
| edge2 = "aaaa\n5\nQ 1 2\nQ 2 4\nI 0 a\nQ 1 3\nR 2 b" | |
| inputs.append(edge2) | |
| # Maximum operations with small string | |
| ops = ["Q 1 1"] * 100 | |
| edge3 = "abc\n100\n" + "\n".join(ops) | |
| inputs.append(edge3) | |
| return inputs | |
Xet Storage Details
- Size:
- 3.15 kB
- Xet hash:
- 9d3211cb7ee6577f55a4146ce0b55be0bc6430241c7bcfa3edc356a0f2c4b6fe
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.