shell脚本实现检測回文字符串
全部回文字的结构特征例如以下:
假设字符数是偶数,那么它在结构上表现为:一个字符序列连着还有一个字符同样但次序恰好相反的字符序列。
假设字符数为奇数,那么它在结构上表现为:一个字符序列连着还有一个字符同样但次序恰好相反的字符序列,可是这两个序列中间共享一个同样的字符。
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脚本实现检測回文字符串的更多相关文章
- 转载-----Java Longest Palindromic Substring(最长回文字符串)
转载地址:https://www.cnblogs.com/clnchanpin/p/6880322.html 假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic st ...
- Java Longest Palindromic Substring(最长回文字符串)
假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic string.如aba,或者abba.本题是这种,给定输入一个字符串.要求输出一个子串,使得子串是最长的padro ...
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 回文字符串的判断!关于strlen(char * str)函数
#include <stdio.h> #include <string.h> int ishuiw(char * p); int main() { ;//true-false接 ...
- NYOJ_37.回文字符串 (附滚动数组)
时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当然,我们给你的问 ...
- 131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串
131. Palindrome Partitioning Given a string s, partition s such that every substring of the partitio ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- 【又见LCS】NYOJ-37 回文字符串
[题目链接] 回文字符串 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba& ...
- [NYOJ 37] 回文字符串
回文字符串 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当 ...
随机推荐
- ASP.NET State Service服务
ASP.NET State Service服务是用来管理 Session 的,正常来说,Session 位于IIS进程中(其实可以理解成在服务器的内存中),当IIS重启或程序池回收会自动清空Sessi ...
- 致命错误: Python.h:没有那个文件或目录
In file included from greenlet.c:5:0: greenlet.h:8:20: 致命错误: Python.h:没有那个文件或目录 编译中断. error: Setup s ...
- Desert King
poj2728:http://poj.org/problem?id=2728 题意:给你n的点,每一个点会有一个坐标(x,y),然后还有一个z值,现在上你求一棵生成树,是的这棵生成树的所有边的费用/所 ...
- [科普贴]为何Flash被淘汰?附Chrome看视频最完美教程!
Adobe 公司放弃 移动 平台的 Flash 支持已经是板上钉钉的事了, Google Play 的 Flash 插件也会在 8 月份下架,这在一定程度上也会促进 HTML5 的发展和普及,因此我个 ...
- 使用HttpClient向服务器发送restful post请求
直接上代码: public class RestClient { public static void main(String[] args) { String url = "http:// ...
- 如何在win下编译thunderbird
最近突然想研究一下thunderbird的实现,于是在WIN2K3下对其进行了系列的编译,特将编译的一些心得与大家共享.其实编译过程已经非常简单了,本文以VC8 ( VISUAL STUDIO 200 ...
- 两个有关Knockout自定义拓展方法fn的小技巧
Adding custom functions using "fn" 让observable自增/自减 最简单的方法是self.num(self.num() + 1), 但是这个写 ...
- [cocos2dx动作]CCLabel类数字变化动作
cococs2dx的CCLabel类的数字变化动作 介绍: 简单的数字变化动作(适用于CCLabel类对象, 包括CCLabelTTF, CCLabelAtlas, CCLabelBMFont等等) ...
- (转载)顺序栈c++实现
(转载)http://myswirl.blog.163.com/blog/static/51318642200882310239324/ SqStack.h********************** ...
- SQL Server 2008 远程过程调用失败[ VS2012]
查看SQL Server配置管理工具,SQL Server 2008 服务中提示“远程过程调用失败”. [解决办法] 首先,核查用户名密码是否正确. 其次,查看sql server 配置管理器中,第一 ...