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.
3 ABCABA XYZ XCVCX
ABA X XCVCX
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;
}
Dr.Kong设计了一个聪明的机器人卡多,卡多会对电子表格中的单元格坐标快速计算出来。单元格的行坐标是由数字编号的数字序号,而列坐标使用字母序号。观察字母序号,发现第1列到第26列的字母序号分别为A,B,…,Z,接着,第27列序号为AA,第28列为AB,依此类推。
若给Dr.Kong的机器人卡多一个数字序号(比如32),它能很快算出等价的字母序号(即AF),若给机器人一个字母序号(比如AA)),它也能很快算出等价的数字序号(27),你能不能与卡多比试比试,看谁能算得更快更准确。
3 27 G AA
AA 7 27
有点坑,,要注意咯,,我们平常转换为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;
}
Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min(20,23)的值是20 ,add(10,98) 的值是108等等。经过训练,Dr.Kong设计的机器人卡多甚至会计算一种嵌套的更复杂的表达式。
假设表达式可以简单定义为:
1. 一个正的十进制数 x 是一个表达式。
2. 如果 x 和 y 是 表达式,则 函数min(x,y )也是表达式,其值为x,y 中的最小数。
3. 如果 x 和 y 是 表达式,则 函数max(x,y )也是表达式,其值为x,y 中的最大数。
4.如果 x 和 y 是 表达式,则 函数add(x,y )也是表达式,其值为x,y 之和。
例如, 表达式 max(add(1,2),7) 的值为 7。
请你编写程序,对于给定的一组表达式,帮助 Dr.Kong 算出正确答案,以便校对卡多计算的正误。
3 add(1,2) max(1,999) add(min(1,1000),add(100,99))
3 999 200
简单栈的应用!!表达式求值!!
一个符号栈,,一个数字栈,以‘)’为一个符号运算结束的标志,,然后算出这个符号内的值,依次算出后数字栈只有一个值了,然后符号栈为空,此时数字栈的那个值即为所求。
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;
}
原文地址:http://blog.csdn.net/u014355480/article/details/42171217