B. Palindrome

    传统题 1000~1500ms 256MiB

Palindrome

该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。

Problem 2. Palindrome

A string s0s1sm1s_0s_1\cdots s_{m-1} is said to be a palindrome if si=sm1is_i=s_{m-1-i} holds for every i{0,1,,m1}i\in\{0,1,\cdots,m-1\}.

This problem consists of two subtasks. See Submission for how to submit your code.

Subtask 1

Read nn strings and test whether each of them is a palindrome.

Input format

On the first line, a nonnegative integer nn.

Then 2n2n lines follow. The (2i1)(2i-1)-th line is a positive integer representing the length of the ii-th string. The 2i2i-th line is the ii-th string, which is guaranteed to contain no whitespace characters.

Output format

nn lines. For every i{1,,n}i\in\{1,\cdots,n\}, print Yes on the ii-th line if the ii-th string is a palindrome; print No otherwise.

Example

Input:

5
1
a
4
(())
2
[[
5
abcba
8
ab0220ba

Output:

Yes
No
Yes
Yes
Yes

Notes

There are no guarantees on the maximum possible value of nn or the length of the strings. Dynamic memory should be used to store each input string.

Subtask 2

Write a program that generates a testcase for subtask 1. You can write it using either C or Python 3. If you use C, the standard library functions rand and srand may be helpful.

The program should accept two command line arguments n and max_length, representing the number of strings and the maximum length of each string, respectively. If you don't remember what command line argument is, go over the slides of Lecture 8.

If you use C, assume your program (the executable, not the C source code file) is named data_gen.exe (or data_gen on Linux/Mac). Your program will be run with a command like the following (with .\ replaced by ./ on Linux/Mac):

.\data_gen 10 20

If you use Python, assume your program is named data_gen.py. Your program will be run with a command like the following:

python data_gen.py 10 20

In either case, your program should print a testcase with n strings, each of which is no longer than max_length characters. For example:

10
4
b82j
7
ll+2MlR
7
e3Erubv
3
o2d
16
u[9H($YttY$(H9[u
3
~<~
20
*]=*o|{"_s6K}GJTbA07
6
:vFEr_
6
f%w>/$
9
%59T`7z+k

It is guaranteed that the values of n and max_length are representable by int.

The generated testcase should contain both palindromes and non-palindromes. We recommend generating a palindrome with 50% probability.

Still, you should use dynamic memory instead of arrays if you use C.

Submission

Your submission should contain two files: one named solution.c for subtask 1, and the other named data_gen.c or data_gen.py for subtask 2. Create a zip file submission.zip containing them, which can be done using the following command (with data_gen.c replaced by data_gen.py if you use Python)

  • For Windows:

    Compress-Archive -Path "solution.c", "data_gen.c" -DestinationPath "submission.zip"
    
  • For Mac/Linux

    zip -r submission.zip solution.c data_gen.c
    

(You need to cd to the directory containing these two files first.)

Submit submission.zip to the OJ.

Testcases 1 ~ 15 are for subtask 1, and the rest are for subtask 2. Note that subtask 2 is run in the stage that the OJ treats as "compile time", so if you see "Compile timeout", it probably means that your program for subtask 2 is too slow.

CS100 Spring2025 Homework 3

未认领
状态
已结束
题目
4
开始时间
2025-3-20 14:00
截止时间
2025-3-28 23:59
可延期
0 小时