USACO Training 1.3 Duel Palindromes

本文介绍了一个程序设计问题:找出大于给定数值S的前N个回文数,这些数需在两种或以上的进制中表现为回文形式。文章提供了完整的C++实现代码,并解释了回文数的概念及判断方法。

A number that reads the same from right to left as when read from left to right is called a palindrome.

和上期一样。回文数呢,就是从左到右从右到左读数都一样的数字。

The number 12321 is a palindrome; the number 77778 is not.

比如12321反过来读还是12321(所以是回文数),但77778就不是。

Of course, palindromes have neither leading nor trailing zeroes, so 0220 is not a palindrome.

但是0在首位或者个位都不算,所以0220不是一个回文数。

The number 21 (base 10) is not palindrome in base 10, but the number 21 (base 10) is, in fact, a palindrome in base 2 (10101).

给一个10进制数下的21,他不是一个回文数。但是2进制下的21(10101)就是一个回文数。

Write a program that reads two numbers (expressed in base 10):

写一个读入两个十进制数的程序,

  • N (1 <= N <= 15)
  • S (0 < S < 10000)。数据范围

and then finds and prints (in base 10) the first N numbers strictly greater than S that are palindromic when written in two or more number bases (2 <= base <= 10).

Solutions to this problem do not require manipulating integers larger than the standard 32 bits.

PROGRAM NAME: dualpal

INPUT FORMAT

A single line with space separated integers N and S.

SAMPLE INPUT (file dualpal.in)

3 25

OUTPUT FORMAT

N lines, each with a base 10 number that is palindromic when expressed in at least two of the bases 2..10. The numbers should be listed in order from smallest to largest.

一共N航,每一行包含一个10进制数,至少两个进制们需要从小到大打印出来。

SAMPLE OUTPUT (file dualpal.out)

26
27
28

/*
ID: choiyin1
PROG: dualpal
LANG: C++
*/
#include <bits/stdc++.h> 
 
using namespace std;
char leter[20]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J'};
void changetheBase(int x,int base,char out[60])
{
    stack<char> S;
    int i=0;
    while(x>0)
    {
        S.push(leter[x%base]);
        x/=base;
    }
    for(i=0;!S.empty();i++)
    {
        out[i]=S.top();
        S.pop();
    }
    out[i]=0;
} 
bool isPalindromic(char input[60])
{
    int temp[60];
    int len=strlen(input);
    for(int i=0;i<len;i++)
    {
        temp[len-i-1]=input[i];
    }
    for(int i=0;i<len;i++)
    {
        if(input[i]!=temp[i])
            return false;
    }
    return true;
}
bool islegal(int x)
{
    char out[60];
    int ans=0;
 
    for(int i=2;i<=10;i++)
    {
        changetheBase(x,i,out);
        if(isPalindromic(out))
        {
            ans++;
        }
        if(ans>=2)
            return true;
    }
    return false;
}
int main()
{
    int N,S,ans;
    freopen("dualpal.in","r",stdin);
    freopen("dualpal.out","w",stdout);
    scanf("%d%d",&N,&S);
    ans=0;
    for(int i=S+1;ans<N;i++)
    {
        if(islegal(i))
        {
            cout<<i<<endl;
            ans++;
        }
    } 
}

USACO1.3中最长回文Calf Flac问题要求在给定的文本中找出最长的回文子串,如果有多个回文长度都等于最大值,输出最前面出现的那一个。以下为该问题的解决方案: ### 解题思路 1. **预处理文本**:去除文本中非字母字符,同时记录每个字母在原文本中的位置,方便后续输出原始回文子串。 2. **遍历文本**:以每个字母为中心,向两边扩展来寻找回文子串。回文子串分为奇数长度和偶数长度两种情况,需要分别处理。 3. **记录最长回文子串**:在遍历过程中,记录最长回文子串的长度和起始位置,同时记录该回文子串在原文本中的起始和结束位置。 4. **输出结果**:输出最长回文子串的长度以及原文本中的最长回文子串。 ### 代码实现 ```python # 读取输入 input_text = input() # 预处理文本,记录字母及其在原文本中的位置 letters = [] positions = [] for i, char in enumerate(input_text): if char.isalpha(): letters.append(char.upper()) positions.append(i) n = len(letters) max_length = 0 start = 0 end = 0 # 遍历每个字母,以其为中心扩展寻找回文子串 for i in range(n): # 奇数长度回文串 left, right = i, i while left >= 0 and right < n and letters[left] == letters[right]: length = right - left + 1 if length > max_length: max_length = length start = positions[left] end = positions[right] left -= 1 right += 1 # 偶数长度回文串 left, right = i, i + 1 while left >= 0 and right < n and letters[left] == letters[right]: length = right - left + 1 if length > max_length: max_length = length start = positions[left] end = positions[right] left -= 1 right += 1 # 输出结果 print(max_length) print(input_text[start:end + 1]) ``` ### 复杂度分析 - **时间复杂度**:$O(n^2)$,其中 $n$ 是文本中字母的数量。因为对于每个字母,都需要向两边扩展来寻找回文子串。 - **空间复杂度**:$O(n)$,主要用于存储字母和其在原文本中的位置。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值