首页 > 其他 > 详细

ZOJ 1188 DNA Sorting

时间:2014-02-27 22:58:41      阅读:635      评论:0      收藏:0      [点我收藏+]

原题链接

题目大意:给定一串字符串,查找字符串里字母逆序排列的对数,按照由少到多的顺序把所有字符串进行排列。

解法:用C++字符串string类的iterator,从每个字符串的起始开始,查找逆序字符的个数,然后用qsort方法按照reverseCount的大小快速排序。

 

参考代码:

#include<iostream>
#include<string>
#include<string.h>
#include<cstdlib>
#include<cstdio>

using namespace std;

struct DNAStr{
	int index,reverseCount;
	string str;
}DNA[102];

int countReverse(string s){
	int num = 0;  
    string::iterator lit = s.begin();  
    string::iterator rit;  
    for(; lit < s.end(); lit++)  
        for(rit = lit + 1; rit <s.end(); rit++)  
        {  
            if(*lit > *rit)  
            {  
                num++;  
            }  
        }  
    return num;  
}
int cmp(const void *a, const void *b){
	DNAStr * x = (DNAStr *)a;
	DNAStr * y = (DNAStr *)b;
	
	return (DNAStr *)x->reverseCount > (DNAStr *)y->reverseCount;
}

int main(){
	int cas,m,n;
	

	cin>>cas;
	while(cas--){
		getchar();
		cin>>n>>m;
		getchar();
		for(int i=0;i<m;i++){
			getline(cin,DNA[i].str);
			DNA[i].index=i;
		}
		
		for(int i=0;i<m;i++)
			DNA[i].reverseCount = countReverse(DNA[i].str);
		qsort(DNA,m,sizeof(DNAStr),cmp);

		for(int i=0;i<m;i++){
			cout<<DNA[i].str<<endl;
		}
		if(cas)
			cout<<endl;
		
	}


	return 0;
}

ZOJ 1188 DNA Sorting,布布扣,bubuko.com

ZOJ 1188 DNA Sorting

原文:http://www.cnblogs.com/naive/p/3568802.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!