Palindromes

Java实现 洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes import java.util.Scanner; public class Main { private static Scanner cin; public static void main(String args[]) throws Exception { cin = new Scanner(System.in); int a = cin.nextInt(); i... 阅读详情

Description
A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string “ABCDEDCBA” is a palindrome because it is the same when the string is read from left to right as when the string is read from right to left.

A mirrored string is a string for which when each of the elements of the string is changed to its reverse (if it has a reverse) and the string is read backwards the result is the same as the original string. For example, the string “3AIAE” is a mirrored string because “A” and “I” are their own reverses, and “3” and “E” are each others’ reverses.

A mirrored palindrome is a string that meets the criteria of a regular palindrome and the criteria of a mirrored string. The string “ATOYOTA” is a mirrored palindrome because if the string is read backwards, the string is the same as the original and because if each of the characters is replaced by its reverse and the result is read backwards, the result is the same as the original string.

Of course,”A”,”T”, “O”, and “Y” are all their own reverses.

A list of all valid characters and their reverses is as follows.

Character Reverse Character Reverse Character Reverse
A A M M Y Y
B N Z 5
C O O 1 1
D P 2 S
E 3 Q 3 E
F R 4
G S 2 5 Z
H H T T 6
I I U U 7
J L V V 8 8
K W W 9
L J X X

Note that O (zero) and 0 (the letter) are considered the same character and therefore ONLY the letter “0” is a valid character.

Input
Input consists of strings (one per line) each of which will consist of one to twenty valid characters. There will be no invalid characters in any of the strings. Your program should read to the end of file.

Output
For each input string, you should print the string starting in column 1 immediately followed by exactly one of the following strings.

STRING CRITERIA
” – is not a palindrome.” if the string is not a palindrome and is not a mirrored string
” – is a regular palindrome.” if the string is a palindrome and is not a mirrored string
” – is a mirrored string.” if the string is not a palindrome and is a mirrored string
” – is a mirrored palindrome.” if the string is a palindrome and is a mirrored string
Note that the output line is to include the -’s and spacing exactly as shown in the table above and demonstrated in the Sample Output below.

In addition, after each output line, you must print an empty line.

Sample Input
NOTAPALINDROME
ISAPALINILAPASI
2A3MEAS
ATOYOTA
Sample Output
NOTAPALINDROME – is not a palindrome.

ISAPALINILAPASI – is a regular palindrome.

2A3MEAS – is a mirrored string.

ATOYOTA – is a mirrored palindrome.
Hint
use the C++’s class of string will be convenient, but not a must
问一串字符是什么样的字符,判断标准:1,是否是回文;2,是否是镜子体文字。
直接写两个函数判断一下就好了,别的没什么。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<queue>
#include<algorithm>
using namespace std;
char a[100];
int check1(int len)
{
    int i, leap;
    for (i = 0; i < len; i++)
    {
        if (a[i] != a[len - i - 1])break;
    }
    if (i == len)return 1;
    else return 0;
}
int check2(int len)
{
    int i, leap;
    for (i = 0; i < len; i++)
    {
        if (a[i] == 'A'&&a[i] == a[len - 1 - i])continue;
        else if (a[i] == 'E'&&a[len - 1 - i] == '3')continue;
        else if (a[i] == 'H'&&a[len - i - 1] == 'H')continue;
        else if (a[i] == 'I'&&a[len - 1 - i] == 'I')continue;
        else if (a[i] == 'J'&&a[len - 1 - i] == 'L')continue;
        else if (a[i] == 'L'&&a[len - 1 - i] == 'J')continue;
        else if (a[i] == 'M'&&a[len - 1 - i] == 'M')continue;
        else if (a[i] == 'O'&&a[len - i - 1] == 'O')continue;
        else if (a[i] == 'S'&&a[len - i - 1] == '2')continue;
        else if (a[i] == 'T'&&a[len - i - 1] == 'T')continue;
        else if (a[i] == 'U'&&a[len - i - 1] == 'U')continue;
        else if (a[i] == 'V'&&a[len - i - 1] == 'V')continue;
        else if (a[i] == 'W'&&a[len - i - 1] == 'W')continue;
        else if (a[i] == 'X'&&a[len - i - 1] == 'X')continue;
        else if (a[i] == 'Y'&&a[len - i - 1] == 'Y')continue;
        else if (a[i] == 'Z'&&a[len - i - 1] == '5')continue;
        else if (a[i] == '1'&&a[len - i - 1] == '1')continue;
        else if (a[i] == '2'&&a[len - i - 1] == 'S')continue;
        else if (a[i] == '3'&&a[len - i - 1] == 'E')continue;
        else if (a[i] == '5'&&a[len - i - 1] == 'Z')continue;
        else if (a[i] == '8'&&a[len - i - 1] == '8')continue;
        else break;
    }
    if (i == len)return 1;
    else return 0;
}
int main()
{
    int i, j, m, n, ans, len, leap1, leap2;
    while (gets(a))
    {
        len = strlen(a);
        leap1 = check1(len);
        leap2 = check2(len);
        cout << a;
        if (leap1&&leap2)
            cout << " -- is a mirrored palindrome." << endl << endl;
        else if (leap1&&!leap2)
            cout << " -- is a regular palindrome." << endl << endl;
        else if (!leap1&&leap2)
            cout << " -- is a mirrored string." << endl << endl;
        else if (!leap1&&!leap2)
            cout << " -- is not a palindrome." << endl << endl;
    }
    return 0;
}
zoj2744 Palindromes 字符串的题 题意是求给一个字符串算出其中有多少个回文子串(每个回文子串必须是连续的),关于这题由于数据范围才5000,所以可以用O(N*N)的动态规划去做或者是用暴力的搜索,每次枚举回文串的对称中点复杂度不超过O(N*N),但是用dp做就很蛋疼了,因为内存限制存不了所有的状态,所以得用滚动数组去存,下面将给出几种不同实现的代码。如果暴力枚举中点方法确实很简单,但我还是建议,写一下dp,/可以锻炼一下代码能了, 阅读详情

相关推荐

无线通信入门:3种键控法(ASK/FSK/PSK)原理图解与实战对比

本文深入解析无线通信中ASK、FSK、PSK三种基础键控法的原理与应用。通过图解对比,阐明幅移键控(ASK)简单低成本但抗干扰差,频移键控(FSK)稳健且应用广泛,相移键控(PSK)高效但复杂。文章结合实战场景,为物联网和嵌入式开发者在选型时提供关键指导,帮助平衡数据速率、成本、功耗与抗干扰需求。

weixin_29274969的博客 187

uva 401 Palindromes

A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from le

七夜 816

LSTM模型股价波动率预测(含Python代码)

源代码文件可在gzh’finance褪黑素'回复【24012203】获取。

celiaweiwei的博客 3774

Palindrome

You are given a string s of n lowercase letters.You have to delete exactly two letters from s , determine whether the string can become a palindrome after the change.Definition of palindrome: If a string s of n ​ lowercase letters is a palindrome, then

小L的博客 231

银河麒麟V10系统下,手把手解决Ollama安装报错‘GLIBCXX_3.4.25 not found’

本文详细介绍了在银河麒麟V10系统下解决Ollama安装报错‘GLIBCXX_3.4.25 not found’的实战指南。通过隔离编译新版libstdc++、环境变量引导和服务化封装等安全方案,确保在不破坏系统稳定性的前提下顺利运行Ollama,适用于国产操作系统与AI工具的兼容性需求。

weixin_42556197的博客 482

光学仿真案例(1) 铌酸锂非线性波导仿真模拟 FDTD仿真

在过去几年里,许多集成光子学应用往往需要多个相干可调谐光源,而且从外部激光器引入额外的光输入增加了芯片封装的复杂性,如果使用基于III-V族、氮化硅(SiN)或铌酸锂(LNO)的集成光源又大大增加了制造复杂性和成本。然而,通过非线性谐波相互作用产生的光源不需要额外的制造工艺来产生增益、反馈和外部电路,即可支持许多应用。在下面的案例中,演示了一种基于Luo[1]的铌酸锂波导频率转换集成系统。该案例主要仿真了利用温度调制实现高效相位匹配的铌酸锂纳米光子波导的二次谐波产生。

q895235273的博客 4142

回文质数 Prime Palindromes(c++题解)

因为 151 既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数。写一个程序来找出范围[a,b](5≤a

hb_zhyu的博客 604

线程池详解(通俗易懂超级好)

目标 【理解】线程池基本概念 【理解】线程池工作原理 【掌握】自定义线程池 【应用】java内置线程池 【应用】使用java内置线程池完成综合案例 线程池 线程池基础 线程池使用 线程池综合案例 4.学员练习 5.线程池总结 概念介绍 1:什么是线程池 2:为什么使用线程池 3:线程池有哪些优势 什么是池 什么是线程池 线程池其实就是一种多线程处理形式,处理过程中可以将任务添加到队列中,然后在创建线程后自动启动这些任务。这里的线程就是我们前面学过的线程,这里的任务就是我们前面学过的实现了Runna.

拉格朗日的学习笔记 12万+

回文词(Palindromes,UVa401) (C++)

输入一个字符串,判断它是否为回文串以及镜像串。输入的字符串中不包括数字0。所谓回文串,就是反转以后与原串相同,如abba和madam。 #include<iostream> #include<cstring> using namespace std; char qq[]="A 3 HIL JM O 2TUVWXY51SE Z 8 "; const char *end[4]={"这不是回文字符串!","这是镜像字符串!","这是回文字符串!","这是回文镜像字符串!"};

二进制宇宙的博客 671

置顶必读(2) |《YOLOv8实战:从入门到深度优化》总目录导航 & 学习路线指南(持续更新中)

🏆 本文收录于 《YOLOv8实战:从入门到深度优化》 专栏。该专栏系统复现并梳理全网各类 YOLOv8 改进与实战案例(当前已覆盖分类 / 检测 / 分割 / 追踪 / 关键点 / OBB 检测等方向),坚持持续更新 + 深度解析,质量分长期稳定在 97 分以上,可视为当前市面上 覆盖较全、更新较快、实战导向极强 的 YOLO 改进系列内容之一。

**My Coding Family** 28万+

【论文复现】——基于NDT与ICP结合的点云配准算法(matlab版)

基于NDT与ICP结合的点云配准算法matlab版

点云侠的博客 490

UVA - 401-Palindromes

题目链接:Palindromes UVA - 401 Palindromes Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu SubmitStatus Description A regular palindrome is a

Never say never 863

基于GA-BP遗传算法优化神经网络的多输入多输出数据预测-Python代码实现

多输入多输出的GA-BP神经网络

2301_80449389的博客 2306

UVa 401 - Palindromes

这题是Rujia Liu推荐的第一个字符串题。 题意很容易看懂,就是给出字符串,判断其是否是回文或镜面对称回文。输出有4种情况,给出的表格做了很清楚的说明。 思路:写两个函数,is_palindrome()判断是否是回文串,is_mirrored()判断是否是镜像对称串。

程书画的 BLOG 4775

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

E. Making Anti-Palindromes(ranting 1600)超详细题解

E. Making Anti-Palindromes(ranting 1600)超详细题解

Bill_HJR的博客 417

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

UVA 11584 Partitioning by Palindromes

题意:  给出一串字符串,要求将它划分成尽量少的字符串,使划分后的所有子串都回文。 解法: 设dp[i]为将0~i划分后的最少回文子串数,有如下状态转移: dp[i] = dp[j] +1,0<=j<i且区间[j+1,i]回文。 /* ********************************************** Author : Nero Created Time:

= = 1002

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

UVa OJ 401-Palindromes

<br />简单字符串处理:<br />{ Author:wzx961008 Problem:UVa 401-Palindromes Verdict:Accepted Language:PASCAL Run Time:0.024s Submission Date:2011-01-12 01:05:27 } const mirrorchar1:string='ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789'; mirrorchar2:strin

Chiwin 538

UVA-401 Palindromes

2016-07-12 UVA - 401 Palindromes 题目大意:判断是否回文、镜面。回文即从左往右与从右往左相同。镜面根据所提供的表格,可能存在非法字符。一开始理解错题意,以为不存在非法字符就是镜面,实际上是从左往右根据表格将全部字符化为镜像后,要与从右往左相同。 解题思路:遍历每个元素,进行回文的判断,是回文的话tag[0] = 1 ,不是的话tag[0]

振丿Love 384

题解:CF1951E No Palindromes

我们考虑将连续的相同字符分为一段,然后将没两段或三段合并,得到答案。分为若干个非回文子串,输出一组可行解或输出。

m0_73020012的博客 965

UVa 401 Palindromes

UVa 401 Palindromes 唔……这个题,抄的书,因为要是我写的话只能暴力了,反正暴力的量也不是很大嘛,但是总有暴力解决不了的时候,所以……我是不是之前就是这么一步步被拉下的? 今天学了个新函数 isalpha(); A regular palindrome is a string of numbers or letters that is the same forward ...

mandiheyanyu的博客 235

Luogu P1217 回文质数 Prime Palindromes

Luogu P1217 回文质数 Prime Palindromes

PanDaoxi 372

USACO双重回文数 Dual Palindromes

双重回文数 Dual Palindromes 题目描述 如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做“回文数”。例如,12321就是一个回文数,而77778就不是。当然,回文数的首和尾都应是非零的,因此0220就不是回文数。 事实上,有一些数(如21),在十进制时不是回文数,但在其它进制(如二进制时为10101)时就是回文数。 编一个程序,从文件读入两个十进制数 N (1 &l...

w13141194211的博客 373

HOJ-1004-Prime Palindromes

题目链接 HOJ-1004-Prime Palindromes 思路分析 一开始想先用欧拉筛筛出所有素数之后在判断是否回文,结果超时。(标准结局) 后来看了下别人的思路,大部分是先自己创造回文数,再判断是否是素数。创造时也有技巧。因为存在一个定理:偶数位回文数除了11外均为合数。 这样就可以只创造 ...

Edith_Finch的博客 337

刷题:Palindromes

题目:Palindromes 题目链接:https://vjudge.net/problem/UVA-401 题解:水题—字符串处理 只需要分别判断镜像串和回文序列 需要注意一个问题,回问序列不需要判断最中间那个, 镜像串需要判断中间那个,满足镜像串的条件。 代码: #include #include #include using namespace std; string s; int fl,fl2; int middle,len; char ch[]={“AEHIJLMOSTUVWXYZ12358”};

qq_45712954的博客 127
上一篇: WERTYU
下一篇: Master-Mind Hints
Techay
博客等级 码龄11年 11粉丝 174原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值