HDOJ/HDU 2163 Palindromes(判断回文串~)

Problem Description
Write a program to determine whether a word is a palindrome. A palindrome is a sequence of characters that is identical to the string when the characters are placed in reverse order. For example, the following strings are palindromes: “ABCCBA”, “A”, and “AMA”. The following strings are not palindromes: “HELLO”, “ABAB” and “PPA”.

Input
The input file will consist of up to 100 lines, where each line contains at least 1 and at most 52 characters. Your program should stop processing the input when the input string equals “STOP”. You may assume that input file consists of exclusively uppercase letters; no lowercase letters, punctuation marks, digits, or whitespace will be included within each word.

Output
A single line of output should be generated for each string. The line should include “#”, followed by the problem number, followed by a colon and a space, followed by the string “YES” or “NO”.

Sample Input
ABCCBA
A
HELLO
ABAB
AMA
ABAB
PPA
STOP

Sample Output

#1: YES
#2: YES
#3: NO
#4: NO
#5: YES
#6: NO
#7: NO

就是简单的判断回文串的问题,遇到STOP结束程序。

import java.util.Scanner;

/**
 * @author 陈浩翔
 * 2016-6-5
 */
public class Main{

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t =1;
        while(sc.hasNext()){
            String str =sc.next();
            if("STOP".equals(str)){
                return;
            }
            System.out.print("#"+(t++)+": ");
            boolean isProgram = true;
            for(int i=0,j=str.length()-1;i<j;i++,j--){
                if(str.charAt(i)!=str.charAt(j)){
                    isProgram=false;
                    break;
                }
            }
            if(isProgram){
                System.out.println("YES");
            }else{
                System.out.println("NO");
            }

        }
    }
}
时间: 2024-07-30 20:24:37

HDOJ/HDU 2163 Palindromes(判断回文串~)的相关文章

HDOJ 2029 Palindromes _easy version(回文串)

Problem Description "回文串"是一个正读和反读都一样的字符串,比如"level"或者"noon"等等就是回文串.请写一个程序判断读入的字符串是否是"回文". Input 输入包含多个测试实例,输入数据的第一行是一个正整数n,表示测试实例的个数,后面紧跟着是n个字符串. Output 如果一个字符串是回文串,则输出"yes",否则输出"no". Sample Input

HDOJ 1282 回文数猜想(回文串类)

Problem Description 一个正整数,如果从左向右读(称之为正序数)和从右向左读(称之为倒序数)是一样的,这样的数就叫回文数.任取一个正整数,如果不是回文数,将该数与他的倒序数相加,若其和不是回文数,则重复上述步骤,一直到获得回文数为止.例如:68变成154(68+86),再变成605(154+451),最后变成1111(605+506),而1111是回文数.于是有数学家提出一个猜想:不论开始是什么正整数,在经过有限次正序数和倒序数相加的步骤后,都会得到一个回文数.至今为止还不知道

代码审查-最大回文串问题,拜托各位了

问题描述 最大回文串问题,拜托各位了 我找很长时间了,就是找不到错误(可以编译,输出不对),拜托各位看一下,新人,没有悬赏分,感谢各位的回答 #include<iostream> #include<string.h> #include<cctype> using namespace std; int main() { char a[101], b[101]; int c[101]; while (cin.getline(a, 101)) { int i,d, k, x,

[百度]2014百度校园招聘之最长回文串

[题目] 给你一个字符串,找出该字符串中对称的子字符串的最大长度.即求最大回文串. [思路1]暴力法 即不使用技巧,穷举所有可能.时间复杂度为O(n^3)(时间上最长,不推荐使用),空间复杂度为O(1). 本思路是从最大长度的字串开始,而不是从最小开始.假如说给定的字符串为len,先遍历长度为len的字串是否为回文串,如果是停止, 如果不是遍历长度为len-1的字串是否是回文串,一次类推. #include <iostream> using namespace std; //是否是回文串 bo

[经典面试题]回文串专题

[小米]2015小米校招之回文数判断 [百度]2014百度校园招聘之最长回文串 [网易]字符串回文分割 [创新工场]2014创新工场校园招聘之回文串修复 [算法]Manacher算法之最大回文子串 [LeetCode]9.Palindrome Number [LeetCode]125.Valid Palindrome [LeetCode]131.Palindrome Partitioning [LeetCode]132.Palindrome Partitioning II

关于c语言回文串的问题

问题描述 关于c语言回文串的问题 问题描述 回文串,是一种特殊的字符串,它从左往右读和从右往左读是一样的.小龙龙认为回文串才是完美的.现在给你一个串,它不一定是回文的,请你计算最少的交换次数使得该串变成一个完美的回文串. 交换的定义是:交换两个相邻的字符 例如mamad 第一次交换 ad : mamda 第二次交换 md : madma 第三次交换 ma : madam (回文!完美!) 输入格式 第一行是一个整数N,表示接下来的字符串的长度(N <= 8000) 第二行是一个字符串,长度为N.

hdu 3068 最长回文

点击打开链接hdu 3068 思路:manacher求解字符串最长回文串 分析:详见点击打开链接 代码: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define MAXN 240010 int ans; char tmpStr[MAXN]; char String[MAXN]; int rad[MAXN]; /

Java实现查找当前字符串最大回文串代码分享_java

先看代码 public class MaxHuiWen { public static void main(String[] args) { // TODO Auto-generated method stub String s = "abb"; MaxHuiWen(s); } //1.输出回文串 public static void MaxHuiWen(String s){ //存储字符串的长度 int length = s.length(); //存储最长的回文串 String M

[LeetCode] Largest Palindrome Product 最大回文串乘积

Find the largest palindrome made from the product of two n-digit numbers. Since the result could be very large, you should return the largest palindrome mod 1337. Example: Input: 2 Output: 987 Explanation: 99 x 91 = 9009, 9009 % 1337 = 987 Note: The