zjut_1208 排列对称串
来源:互联网 发布:2016年双十一销售数据 编辑:程序博客网 时间:2024/05/16 14:05
Description:
123321
123454321
123
321
sdfsdfd
121212
123321
\\dd\\
很多字串,有些是对称的,有些是不对称的,请将那些对称的字串按从小到大的顺序输出。字串先以长度论大小,如果长度相同,再以ASCII码值为大小标准。
输入数据中含有一些字串(1≤串长≤256)。
根据每个字串,输出对称的那些串,并且要求按从小到大的顺序输出。
123321
123454321
123
321
sdfsdfd
121212
\\dd\\
123321
\\dd\\
123454321
题解:
判断字符串是否对称,这里可以用一种巧妙的办法,就是把字符串倒置,看是否跟原来相同。这要用到<algorithm>里面的reverse函数。
把对称的字符串装到vector里面后,就要对它们进行排序了,可以使用sort函数,时间复杂度为 n*log2(n) 。
之前我们用的sort函数只有两个参数,即sort(v.begin(),v.end());这个只能用来给整数排序;
现在要给string排序,要使用含有三个参数的sort函数,即sort(v.begin(),v.end(),Comp);其中Comp是自定义的排序函数。
代码:
#include <iostream>#include <string>#include <algorithm>#include <vector>using namespace std;bool Comp(const string &s1, const string &s2){ return s1.length()!=s2.length()?s1.length()<s2.length():s1<s2;}int main(){ vector<string>v; string t,s; while(cin>>s) { t=s; reverse(t.begin(),t.end()); if(t==s) { v.push_back(s); } } sort(v.begin(),v.end(),Comp); for(int i=0;i<v.size();i++) { cout<<v[i]<<endl; } return 0;}
0 0
- zjut_1208 排列对称串
- 3.15 排列对称串
- ZJUT 1208 排列对称串
- 对称排列
- POJ 2013 对称排列
- 对称位置排列
- POJ-2013 Symmetric Order-对称排列人名
- LeetCode 266. Palindrome Permutation(对称排列)
- LeetCode 267. Palindrome Permutation II(对称排列)
- 最长对称子串
- 最长对称子串
- 求对称子串
- 最长对称子串
- 最长对称子串
- 最长对称子串
- 最长对称子串
- 最长对称子串
- 最长对称子串
- 阅读nginx源码_win32
- 谧中壮粱辟葱椭始承梢咳么澜诚跋
- jquery 读取页面表格中的数据 生成批量insert代码 并以ajax方式传给后台
- skynet学习笔记【skynet multicast服务的实现】
- 1026. 程序运行时间(15)
- zjut_1208 排列对称串
- 写出一个二叉树类,也就是C++中的模板类,后面加一个二叉树接口口,即c++中的类模板,最后再来一个二叉树结点类
- 德国地方个地方个地方个地方过放电
- 十分士大夫似的发生的地方
- a20 uart0 test demo
- lcc源代码解析之lex.c
- 子集生成 -- 二进制法
- 硬盘的读写原理
- 折半查找(待改进)