zjfc---1120 对称串

本文介绍了一种用于处理和排序对称字符串的算法实现。该算法读取多个字符串,筛选出对称字符串,并使用stable_sort对其进行长度升序排序。文章包含完整的C++代码示例。

题目描述

有一些字串,有些对称,有些不对称,请将对称的串按长度排列。

输入描述

输入为多行,每行一个串,串的长度不超过200,处理到文件结束

输出描述

输出对称的串,并且按串的长度升序排列,如果串长度相同,则按输入的顺序输出

样例输入

123321
123454321
123
321
sdfsdfd
121212
\\dd\\

样例输出

123321
\\dd\\
123454321
代码如下(用到stable_sort)
ContractedBlock.gifExpandedBlockStart.gifCode
#include<iostream>
#include
<algorithm>
#include
<string>
#include
<vector>
using namespace std;
vector 
<string> s;
bool cmp(const string &a,const string &b)
{
    
return a.size()<b.size();
}
int main()
{
    
int n,i;
    
char tmp[202],a[202];
    n
=0;
    
while(gets(tmp)!=NULL){
        strcpy(a,tmp);
        strrev(a);
        
if(strcmp(a,tmp)==0)
            s.push_back(tmp);
    }
    stable_sort(s.begin(),s.end(),cmp);
    
for(i=0;i<s.size();i++)
        cout
<<s[i]<<endl;
    
return 0;
}

转载于:https://www.cnblogs.com/pandy/archive/2009/05/01/1447375.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值