ZOJ Problem Set - 1188 DNA Sorting
来源:互联网 发布:淘宝折扣返利网 编辑:程序博客网 时间:2024/06/09 06:04
One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to its right and E is greater than one letter to its right. This measure is called the number of inversions in the sequence. The sequence ``AACEDGG'' has only one inversion (E and D)--it is nearly sorted--while the sequence ``ZWQM'' has 6 inversions (it is as unsorted as can be--exactly the reverse of sorted).
You are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of ``sortedness'', from ``most sorted'' to ``least sorted''. All the strings are of the same length.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Input
The first line contains two integers: a positive integer n (0 < n <= 50) giving the length of the strings; and a positive integer m (1 < m <= 100) giving the number of strings. These are followed by m lines, each containing a string of length n.
Output
Output the list of input strings, arranged from ``most sorted'' to ``least sorted''. If two or more strings are equally sorted, list them in the same order they are in the input file.
Sample Input
1
10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT
Sample Output
CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA
Source: East Central North America 1998
分析:
字符串的排序问题。
题意:
给若干字符串。对于某一字符串,固定某个字符,向右扫描,若它的右边有n个字符比它大,则sum+=n。把字符串的所有字符都照此规则扫描一遍,得到的sum就是字符串的“倒位数量”。对于给定的若干字符串,要求按照sum的大小从小到大输出。
输入有多个块,每个块之间输出一个换行。
ac代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
vector<string> v;
string s;
bool cmp(const string &s1,const string &s2)//重点是排序函数,熟悉写法
{
int i,j,k,c1=0,c2=0;
int L1=s1.length(),L2=s2.length();
for(i=0;i<L1;i++)
for(j=i+1;j<L1;j++)
{
if(s1[i]>s1[j])
c1++;
}
for(i=0;i<L2;i++)
for(j=i+1;j<L2;j++)
{
if(s2[i]>s2[j])
c2++;
}
return c1<c2;
}
int main()
{
int t,n,m,i,j;
scanf("%d",&t);
int c=0;
while(t--)
{
scanf("%d%d",&n,&m);
v.clear();//注意清空vector容器
for(i=0;i<m;i++)
{
cin>>s;
v.push_back(s);
}
if(c) printf("\n");
sort(v.begin(),v.end(),cmp);
for(i=0;i<m;i++)
cout<<v[i]<<endl;
c++;
}
return 0;
}
- ZOJ Problem Set - 1188 DNA Sorting
- ZOJ Problem Set - 1188 DNA Sorting
- ZOJ 1188 DNA Sorting
- ZOJ 1188 DNA Sorting
- zoj 1188 DNA Sorting
- zoj 1188 - DNA Sorting
- ZOJ Problem Set - 1171 Sorting the Photos
- Solution of ZOJ 1188 DNA Sorting
- zoj 1188 DNA Sorting(STL)
- zoj 1188.DNA Sorting
- ZOJ Problem Set - 1060 Sorting It All Out
- Problem E-5 DNA sorting
- zoj 1188 || poj 1007 DNA Sorting(水~)
- ZOJ Problem Set - 2060
- ZOJ Problem Set - 2972
- ZOJ Problem Set - 1037
- ZOJ Problem Set - 1048
- ZOJ Problem Set - 1049
- PhotoView开源项目剖析
- Sublime Text 3常用插件安装(含Emmet插件指令介绍)
- Python的__name__与__main__函数
- backbone(-2) 理解面向对象
- linux下tomcat init脚本
- ZOJ Problem Set - 1188 DNA Sorting
- 提高Java中锁的性能
- mysql order by field 按指定字段排序
- uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型 - 大总结,看完全明白了
- nginx新手配置$document_root
- [C++]LeetCode: 118 Find Peak Element (二分查找 寻找数组局部峰值)
- 并查集模版
- web_find()和web_reg_find()的区别
- linux 环境下 c语言实现mysql数据库图片的存储以及多数据库直接的转存