poj 1007 DNAsorting

1.这道题目其实蛮简单的,只是在排序的时候卡了一下,本来打算找一下能不能用C++自带的快速排序解决的(qsort)但是qsort定义的格式和我传入的格式不符合,实在受不了,就自己直接用快速排序实现了一下大笑

2.还是自己太弱了,网上一搜发现有很多用sort,qsort做的,果然自己还是太弱小了委屈

http://blog.csdn.net/lyy289065406/article/details/6647305

http://blog.csdn.net/woshixingaaa/article/details/5589070




Source Code

Problem: 1007 User: zhyh2010
Memory: 220K Time: 16MS
Language: C++ Result: Accepted
  • Source Code
    #include <iostream>
    #include <map>
    using namespace std;
    
    typedef struct myMap
    {
    	int first;
    	int second;
    }mYMap,*pmYMap;
    
    int partition(pmYMap pA, int p, int r)
    {
    	int data = pA[r].second;
    	int i = p - 1;
    	for (int j = p; j != r; ++j)
    	{
    		if (pA[j].second < data)
    		{
    			i++;
    			// exchange
    			mYMap temp;
    			temp.first = pA[i].first;
    			temp.second = pA[i].second;
    			pA[i].first = pA[j].first;
    			pA[i].second = pA[j].second;
    			pA[j].first = temp.first;
    			pA[j].second = temp.second;
    		}
    	}
    	mYMap temp;
    	temp.first = pA[i+1].first;
    	temp.second = pA[i+1].second;
    	pA[i+1].first = pA[r].first;
    	pA[i+1].second = pA[r].second;
    	pA[r].first = temp.first;
    	pA[r].second = temp.second;
    	return i + 1;
    }
    
    void quicksort(pmYMap pA, int p, int r)
    {
    	if (p < r)
    	{
    		int q = partition(pA, p, r);
    		quicksort(pA, p, q - 1);
    		quicksort(pA, q + 1, r);
    	}
    }
    
    
    int main(int argc, char * argv[])
    {
    	int length;
    	int num;
    	cin >> length >> num;
    	//int* m_pindex = new int[num];
    	pmYMap m_pindex = new mYMap[num];
    	char ** m_pstr = new char*[num];
    	for (int i = 0; i != num; ++i)
    	{
    		m_pstr[i] = new char[length + 1];
    	}
    	for (int i = 0; i != num; ++i)
    	{
    		for (int j = 0; j != length; ++j)
    		{
    			cin >> m_pstr[i][j];
    		}
    		m_pstr[i][length] = '\0';
    
    		// handle
    		int count = 0;
    		for (int ia = 0; ia != length; ++ia)
    		{
    			for (int ib = ia + 1; ib != length; ++ib)
    			{
    				if (m_pstr[i][ia] > m_pstr[i][ib])
    				{
    					count++;
    				}
    			}
    		}
    		//m_pindex[i] = count;
    		m_pindex[i].first = i;
    		m_pindex[i].second = count;
    	}
    
    	// quick sort
    	quicksort(m_pindex, 0, num - 1);
    
    	// output
    	for (int i = 0; i != num; ++i)
    	{
    		cout << m_pstr[m_pindex[i].first] << endl;
    		//cout << m_pindex[i].first <<m_pindex[i].second<< endl;
    	}
    
    	return 0;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值