全部回文字的结构特征例如以下:

假设字符数是偶数,那么它在结构上表现为:一个字符序列连着还有一个字符同样但次序恰好相反的字符序列。

假设字符数为奇数,那么它在结构上表现为:一个字符序列连着还有一个字符同样但次序恰好相反的字符序列,可是这两个序列中间共享一个同样的字符。

sed命令可以记住之前匹配的子样式。

可以用正則表達式:'\(.\)'。匹配随意一个字符。\1表示其反向引用。如匹配有两个字符的回文正則表達式为:

'\(.\)\(.\)\2\1'

匹配随意长度的回文脚本例如以下所看到的:

#!/bin/bash
#file name: match_palindrome.sh
#function: find palindrome in a file. if [ $# -ne 2 ]
then
echo "Usage: $0 filename string_length"
exit -1
fi filename=$1 basepattern='/^\(.\)' count=$(( $2/2 )) # matche certain length
for ((i=1; i < $count; i++))
do
basepattern=$basepattern'\(.\)';
done # the length is even
if [ $(( $2 % 2)) -ne 0 ]
then
basepattern=$basepattern'.';
fi for ((count; count > 0; count--))
do
basepattern=$basepattern'\'"$count";
done echo "debug: $basepattern" # print the result
basepattern=$basepattern'$/p'
sed -n "$basepattern" $filename

shell脚本实现检測回文字符串的更多相关文章

  1. 转载-----Java Longest Palindromic Substring(最长回文字符串)

    转载地址:https://www.cnblogs.com/clnchanpin/p/6880322.html 假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic st ...

  2. Java Longest Palindromic Substring(最长回文字符串)

    假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic string.如aba,或者abba.本题是这种,给定输入一个字符串.要求输出一个子串,使得子串是最长的padro ...

  3. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  4. 回文字符串的判断!关于strlen(char * str)函数

    #include <stdio.h> #include <string.h> int ishuiw(char * p); int main() { ;//true-false接 ...

  5. NYOJ_37.回文字符串 (附滚动数组)

    时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当然,我们给你的问 ...

  6. 131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串

    131. Palindrome Partitioning Given a string s, partition s such that every substring of the partitio ...

  7. leetcode:Longest Palindromic Substring(求最大的回文字符串)

    Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...

  8. 【又见LCS】NYOJ-37 回文字符串

    [题目链接] 回文字符串 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba& ...

  9. [NYOJ 37] 回文字符串

    回文字符串 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当 ...

随机推荐

  1. iOS - 发送邮件

    IOS系统框架提供的两种发送Email的方法:openURL 和 MFMailComposeViewController.借助这两个方法,我们可以轻松的在应用里加入如用户反馈这类需要发送邮件的功能. ...

  2. spring 下载地址(拷贝)

    Spring2.5.6 和Spring3.0.5所有jar下载地址spring jar包 官方下载地址 文档下载地址.2.56版本 和3.05版本http://s3.amazonaws.com/dis ...

  3. http://rogerdudler.github.io/git-guide/index.zh.html

    http://rogerdudler.github.io/git-guide/index.zh.html

  4. UVA 10034 Freckles 最小生成树

    虽然是道普通的最小生成树题目,可还是中间出了不少问题,暴露的一个问题是不够细心,不够熟练.所以这篇博客就当记录一下bug吧. 代码一:kruskal #include<stdio.h> # ...

  5. Android用户界面UI组件--AdapterView及其子类(四) GridView

    GridView常用的XML属性: android:columnWidth  设置列的宽度. android:horizontalSpacing  两列之间的间距.  android:numColum ...

  6. 12.URL重写

    为什么要URL重写?1.有利于SEO(搜索引擎优化),带参数的RUL权重较低.2.地址看起来更正规,推广uid. 如我们一般在访问网页是会带参数,http://aaa.com/view.htm?id= ...

  7. B*tree dump

    Oracle的索引是以平衡树的方式组织存储的:保存的是索引列的值,以及该行的rowid的一部分(文件号,块号,行号) 下面我们通过例子来了解一下: 1,create table test(id int ...

  8. 【canvas】伸缩 / 剪裁 / 文本 / 阴影 / 填充图案 / 填充渐变

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  9. eclipse环境NDK问题汇总

    1. 配置NDK路径设置 可以在cygwin中通过vim修改,也可以在windows安装目录中修改 home\<你的用户名>\.bash_profile 文件中最后添加环境变量 NDK=/ ...

  10. c# const与readonly 关键字的比较

    C#中,const 与readonly是两个比较有用的关键字.const 与 readonly 定义的数据成员在初始化都不能再改变. 比如定义了 public class MathUtitlity   ...