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".当 ...
随机推荐
- 服务器慢 mysql-bin.000001文件占满磁盘的原因与解决
发现 VPS 服务器上的网站反应超级慢,简单的重启.重启各主要服务,发现mysql 的反应极其不正常. 一方面是问题,这与站点访问量有关.开始时从mysql 的配置文件 my.cnf 考虑,但志文工作 ...
- STM32之SD卡
目录 一.SD卡概述 1.定义 2.容量等级 3.SD卡框图 4.SD卡与TF卡的区别 二. SD卡内部结构 1. SD卡内部结构简图 2. 存储阵列结构图 3.Buffer 4.“存储阵列Block ...
- OC 之 const
1. 修饰变量 一般设置传参数的时候 若设置为const, 则在调用过程中不允许修改参数值;(readonly) // *前const: 不能通过指针, 改变p指向的值 const int *p = ...
- leetcode 第五题 Longest Palindromic Substring (java)
Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...
- delphi xe5 android 手机上使用sqlite
本篇我们介绍一下在android手机上怎样使用sqlite数据库,这里用Navigator实现 增删改查. 1.新建firemonkey mobile application 2.选择blank ap ...
- BZOJ 1754: [Usaco2005 qua]Bull Math
Description Bulls are so much better at math than the cows. They can multiply huge integers together ...
- Dirichlet Process 和 Dirichlet Process Mixture模型
Dirichlet Process 和 Dirichlet Process Mixture模型 [本文链接:http://www.cnblogs.com/breezedeus/archive/2012 ...
- Codeforces Round #198 (Div. 2) —— D
昨天想了一下D题,有点思路不过感觉很麻烦,就懒得去敲了: 今天上午也想了一下,还是没有结果,看了一下官方题解,证明得很精彩: 这道题目其实就是一道裸地最大上升子序列的题: 看到这里,直接怒码···· ...
- weixin
http://gps.yesky.com/19/34467019.shtml http://***/goods.php?id=320 http://www.sablog.net/blog/archiv ...
- [mock]10月4日
第一次mock,CollabEdit开一个页面,开始做题.题目是,有方法pow(m,n),m和n都大于1,给出N,有顺序的打印出前N个pow(m,n)的结果.前一个是:4,8,9,16,... 然后在 ...