历年上海交通大学计算机保研上机真题
2025上海交通大学计算机保研上机真题
2024上海交通大学计算机保研上机真题
2023上海交通大学计算机保研上机真题
在线测评链接:https://pgcode.cn/school
String Match
题目描述
Finding all occurrences of a pattern in a text is a problem that arises frequently in text-editing programs. Typically, the text is a document being edited, and the pattern searched for is a particular word supplied by the user. We assume that the text is an array T[1…n] of length n and that the pattern is an array P[1…m] of length m <= n. We further assume that the elements of P and T are all alphabets (∑ = {a, b, …, z}). The character arrays P and T are often called strings of characters.
We say that pattern P occurs with shift s in the text T if 0 <= s <= n and T[s+1…s+m] = P[1…m] (that is, if T[s+j] = P[j], for 1 <= j <= m). If P occurs with shift s in T, then we call s a valid shift; otherwise, we call s an invalid shift. Your task is to calculate the number of valid shifts for the given text T and pattern P.
输入格式
For each case, there are two strings T and P on a line, separated by a single space. You may assume both the length of T and P will not exceed 10^6.
输出格式
You should output a number on a separate line, which indicates the number of valid shifts for the given text T and pattern P.
输入样例
abababab abab
输出样例
3
二次方程计算器
题目描述
设计一个二次方程计算器,用于求解给定的二次方程。
输入格式
每个输入是关于 x 的一个二次方程表达式,为了简单,每个系数都是整数形式。
输出格式
每个案例输出两个实数(由小到大输出,中间由空格隔开),保留两位小数;如果无解,则输出"No Solution"。
输入样例
x^2+x=3x+4
输出样例
-1.24 3.24
WERTYU
题目描述
A common typing error is to place the hands on the keyboard one row to the right of the correct position. So “Q” is typed as “W” and “J” is typed as “K” and so on. You are to decode a message typed in this manner.
输入格式
Input consists of several lines of text. Each line may contain digits, spaces, upper case letters (except Q, A, Z), or punctuation shown above [except back-quote (`)]. Keys labelled with words [Tab, BackSp, Control, etc.] are not represented in the input.
输出格式
You are to replace each letter or punctuation symbol by the one immediately to its left on the QWERTY keyboard shown above. Spaces in the input should be echoed in the output.
输入样例
O S, GOMR YPFSU/
输出样例
I AM FINE TODAY.
Simple Sorting
题目描述
You are given an unsorted array of integer numbers. Your task is to sort this array and remove possible duplicated elements occurring in it.
输入格式
For each case, the first line of the input contains an integer number N representing the quantity of numbers in this array (1 ≤ N ≤ 1000). Next N lines contain N integer numbers (one number per each line) of the original array.
输出格式
For each case, output file should contain at most N numbers sorted in ascending order. Every number in the output file should occur only once.
输入样例
6
8
8
7
3
7
7
输出样例
3 7 8
Old Bill
题目描述
Among grandfather’s papers a bill was found:
“72 turkeys $679”
The first and last digits of the number representing the total price are faded (denoted by _). What are the two faded digits and what was the price per turkey?
We want to write a program that solves a general version of this problem:
“N turkeys $XYZ”
- N (number of turkeys) is between 1 and 99 (inclusive)
- The total price originally had 5 digits, but only the middle 3 digits (X, Y, Z) are visible
- The first digit is nonzero
- Each turkey costs the same integer price
Given N, X, Y, Z, find the two faded digits and the original total price. If multiple solutions exist, choose the most expensive one (maximum price per turkey).
输入格式
The first line contains integer N (0 < N < 100). The second line contains three digits X, Y, Z separated by spaces (representing the middle three digits of the total price).
输出格式
Output the two faded digits (first and last) followed by the maximum price per turkey (integer).
输入样例
72
6 7 9
输出样例
3 5 11
Fibonacci
题目描述
The Fibonacci Numbers {0,1,1,2,3,5,8,13,21,34,…} are defined by the recurrence:
F₀ = 0, F₁ = 1, Fₙ = Fₙ₋₁ + Fₙ₋₂ (n ≥ 2).
Write a program to calculate the Fibonacci Numbers.
输入格式
Each case contains a number n, and you are expected to calculate Fₙ (0 ≤ n ≤ 30).
输出格式
For each case, print a number Fₙ on a separate line, which means the nth Fibonacci Number.
输入样例
1
输出样例
1
数字反转
题目描述
12 翻一下是 21,34 翻一下是 43,12+34 是 46,46 翻一下是 64。
现在有两个任意正整数,问这两个数反转的和是否等于两个数的和的反转。
输入格式
每行两个正整数 a 和 b(0 < a, b <= 10000)。
输出格式
如果满足题目的要求输出 a+b 的值,否则输出 NO。
输入样例
12 34
99 1
输出样例
46
NO
涂颜色
题目描述
在这个游戏中,有一个 n 行 m 列的方阵。
现在要为这个方阵涂上黑白两种颜色。
规定左右相邻两格的颜色不能相同。
请你统计有多少种涂色的方法。
由于答案很大,你需要将答案对 1000000007 取模。
1≤n,m≤10^100000
输入格式
输入两个数 n,m
输出格式
输出总共的方案数
输入样例
2 2
输出样例
4
最长平衡子串
题目描述
我们定义满足最多由两种字符组成,并且字符串中两个字符的数量之差的绝对值小于等于 k 的字符串为 k 阶平衡字符串。
例如 abb 就是 1 阶平衡字符串,也是 2 阶平衡字符串。
现在给出一个长度为 n 并且最多由两种字母字符组成的字符串 s。
对于每次询问 qi,求 s 字符串的所有子串中最长的 qi 阶平衡字符串的长度。
输入格式
多组测试数据,对于每组测试数据包含 m+2 行:
- 第 1 行包含两个整数 n,m(最大 10^5)
- 第 2 行是一个长度为 n 的字符串 s
- 接下来 m 行,每行一个整数 qi 表示询问
输出格式
对于每组测试数据的每次询问,输出一个整数表示原字符串的子串中最长的 qi 阶平衡字符串的长度。
输入样例
5 2
baaaa
2
1
输出样例
4
3
最小面积子矩阵
题目描述
一个 N*M 的矩阵,找出这个矩阵中所有元素的和不小于 K 的面积最小的子矩阵(矩阵中元素个数为矩阵面积)。
输入格式
每个案例第一行三个正整数 N,M<=100,表示矩阵大小,和一个整数 K。
接下来 N 行,每行 M 个数,表示矩阵每个元素的值。
输出格式
输出最小面积的值。
如果任意矩阵的和都小于 K,直接输出-1。
输入样例
4 4 10
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
输出样例
1