第四届河南省程序设计大赛 - 部分题解

来源:互联网 发布:2005sql安装步骤 编辑:程序博客网 时间:2024/04/27 16:00

Substring

时间限制:1000 ms  |  内存限制:65535 KB
难度:1
描述

You are given a string input. You are to find the longest substring of input such that the reversal of the substring is also a substring of input. In case of a tie, return the string that occurs earliest in input. 

Note well: The substring and its reversal may overlap partially or completely. The entire original string is itself a valid substring . The best we can do is find a one character substring, so we implement the tie-breaker rule of taking the earliest one first.

输入
The first line of input gives a single integer, 1 ≤ N ≤ 10, the number of test cases. Then follow, for each test case, a line containing between 1 and 50 characters, inclusive. Each character of input will be an uppercase letter ('A'-'Z').
输出
Output for each test case the longest substring of input such that the reversal of the substring is also a substring of input
样例输入
3                   ABCABAXYZXCVCX
样例输出
ABAXXCVCX
来源
第四届河南省程序设计大赛


AC代码:

#include <cstdio>#include <cstring>using namespace std; int main(){int n, k;char a[55], b[55], c[55][55];scanf("%d", &n);while(n--){int max=0;memset(c, 0, sizeof(c));scanf("%s", a);int len = strlen(a);for(int i = 0; i < len; i++)b[i] = a[len-1-i];for(int i = 1; i <= len; i++)for(int j = 1; j <= len; j++)if(a[i-1] == b[j-1]){c[i][j] = c[i-1][j-1] + 1;if(max < c[i][j]){max = c[i][j];k = i;}}for(int i = k-max; i < k; i++)printf("%c", a[i]);printf("\n");}return 0;}





序号互换

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
描述

Dr.Kong设计了一个聪明的机器人卡多,卡多会对电子表格中的单元格坐标快速计算出来。单元格的行坐标是由数字编号的数字序号,而列坐标使用字母序号。观察字母序号,发现第1列到第26列的字母序号分别为A,B,…,Z,接着,第27列序号为AA,第28列为AB,依此类推。

若给Dr.Kong的机器人卡多一个数字序号(比如32),它能很快算出等价的字母序号(即AF),若给机器人一个字母序号(比如AA),它也能很快算出等价的数字序号(27),你能不能与卡多比试比试,看谁能算得更快更准确。

输入
第一行: N 表示有多少组测试数据。 
接下来有N行, 每行或者是一个正整数,或者是一个仅由大写字母组成的字符串。
输入保证,所有数字序号和字母序号对应的数字序号均 ≤ 2*10^9
输出
对于每一行测试数据,输出一行。如果输入为一个正整数序号,则输出等价的字母序号;如果输入为字符串,则输出等价的数字序号。
样例输入
327GAA
样例输出
AA727
来源
第四届河南省程序设计大赛


有点坑,,要注意咯,,我们平常转换为10进制只要那个num%10就OK了,,但是这里要注意A是代表1,是从1开始的,而10进制是0开始的,所以我们要先减去1再取模


AC代码:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int main(){char a[20];int n;scanf("%d", &n); while(n--){scanf("%s", a);if(a[0]<='9' && a[0]>='0'){int num=0, len = strlen(a);for(int i=0; i<len; i++)num = num*10 + a[i]-'0';char ans[22], *p=ans+20;ans[21] = '\0';while(num){*p = (num-1)%26+'A';num = (num-1)/26;p--;}printf("%s\n", p+1);}else {int len = strlen(a), ans=0, k = 1;for(int i=len-1; i>=0; i--){ans += (a[i]-'A'+1)*k;k *= 26;}printf("%d\n", ans);}}return 0;} 






表达式求值

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述

Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min(20,23)的值是20 ,add(10,98) 的值是108等等。经过训练,Dr.Kong设计的机器人卡多甚至会计算一种嵌套的更复杂的表达式。

假设表达式可以简单定义为:

1. 一个正的十进制数 x 是一个表达式。

2. 如果 和  表达式,则 函数min(x,y )也是表达式,其值为x,y 中的最小数。

3. 如果 和  表达式,则 函数max(x,y )也是表达式,其值为x,y 中的最大数。

4.如果 和  表达式,则 函数add(x,y )也是表达式,其值为x,y 之和。

例如, 表达式 max(add(1,2),7) 的值为 7。

请你编写程序,对于给定的一组表达式,帮助 Dr.Kong 算出正确答案,以便校对卡多计算的正误

输入
第一行: N 表示要计算的表达式个数 (1≤ N ≤ 10) 
接下来有N行, 每行是一个字符串,表示待求值的表达式
(表达式中不会有多余的空格,每行不超过300个字符,表达式中出现的十进制数都不
超过1000。)
输出
输出有N行,每一行对应一个表达式的值。
样例输入
3add(1,2) max(1,999) add(min(1,1000),add(100,99)) 
样例输出
3999200
来源
第四届河南省程序设计大赛


简单栈的应用!!表达式求值!!


一个符号栈,,一个数字栈,以‘)’为一个符号运算结束的标志,,然后算出这个符号内的值,依次算出后数字栈只有一个值了,然后符号栈为空,此时数字栈的那个值即为所求。


AC代码:

#include <cstdio>#include <cstring>#include <algorithm>#include <stack>using namespace std;char a[305];int main(){int n;scanf("%d", &n);while(n--){scanf("%s", a);stack<int> num;stack<int> str;int len = strlen(a);for(int i=0; i<len; i++){if(a[i]<='9' && a[i]>='0') {int t = 0;while(a[i]<='9' && a[i]>='0'){t = t*10+a[i]-'0';i++;}i--;num.push(t);}else if(a[i]=='a' && a[i+1]=='d') str.push(3);else if(a[i]=='m' && a[i+1]=='i') str.push(1);else if(a[i]=='m' && a[i+1]=='a') str.push(2);else if(a[i]==')'){int pan = str.top(); str.pop();int a = num.top(); num.pop();int b = num.top(); num.pop();int c;if(pan==1) c=min(a,b);else if(pan==2) c=max(a,b);else if(pan==3) c=a+b;num.push(c);}}printf("%d\n", num.top());}return 0;} 




1 0
原创粉丝点击