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".当 ...
随机推荐
- SQL Server 2008 R2 主从数据库同步设置
一.准备工作: 主数据库服务器: OS:Windows Server 2008 R2 DB: SQL Server 2008 R2 Hostname : CXMasterDB IP: 192.1 ...
- NULL, nil, Nil详解
原文地址:http://blog.csdn.net/wzzvictory/article/details/18413519 感谢原作者 作者:wangzz 原文地址:http://blog.cs ...
- 隐藏 Status Bar
iOS6和iOS7在隐藏 Status Bar 三种方式比较: Storyboard 界面上选中UIViewController,最右边Simulated Metrics找到 Status Bar 设 ...
- 使用XmlDocument.SelectNodes遍历xml元素遇到的一个XPathException
使用XmlDocument类时候报错: 未处理的XPathException:需要命名空间管理器或 XsltContext.此查询具有前缀.变量或用户定义的函数. 需要使用XmlNamespaceMa ...
- PS仿制图章
颜色总不对,和周围不太搭配,这时候把流量调小点,然后用渐变工具.自己实践所得.
- uvalive 6185
高斯消元,以前从来没写过,今天的模拟比赛里面,添琦给了我一个模板! 虽然是个裸的,但是因为从来没写过,一个小细节竟然搞了我几个小时: 终于最后在小珺同志的帮助下成功a掉了,太开心了! 存一下,作为模板 ...
- leetcode面试准备:Valid Anagram
leetcode面试准备:Valid Anagram 1 题目 Given two strings s and t, write a function to determine if t is an ...
- PEP Index > PEP 339 -- Design of the CPython Compiler 译文
http://www.python.org/dev/peps/pep-0339/ PEP: 339 标题: CPython的编译器设计 版本: 425fc5598ee8 最后修改: 2011-01-1 ...
- Sharepoint 2010 之 WebPart
转:http://blog.csdn.net/bestbadgod/article/details/6895542 Sharepoint系列的博客,都是我个人自学过程中的点滴的积累,毕竟没做过C#及A ...
- HDU-1391 Number Steps
http://acm.hdu.edu.cn/showproblem.php?pid=1391 Number Steps Time Limit: 2000/1000 MS (Java/Others) ...