北大OJ_1007题:DNA Sorting

该题的意思是输入指定数量的字符串,每个字符串的长度一样,找出每个字符串中逆序对,然后按逆序对的升序输出所以的字符串,逆序对相同的则按输入时的顺序输出。

此题的突破点在找逆序对,以下列举两种找出逆序对的方法。

穷举法找逆序对(时间复杂度为O(n^2))

#include <iostream>
#include <string>
#include <deque>
#include <cmath>

using namespace std;

struct tagString
{
	int nInvertedCount;
	string strSequence;
};

int main()
{
	deque<tagString> mStrList;
	tagString mTagString;
	short n,m;
	cin >> n >> m;
	while( m > 0 )
	{
		cin >> mTagString.strSequence;
		int nInvertedCount = 0;
		for( string::size_type i = 0; i < mTagString.strSequence.size() - 1; ++i )
		{
			for( string::size_type j = i + 1; j < mTagString.strSequence.size(); ++j )
			{
				if ( mTagString.strSequence[i] > mTagString.strSequence[j] )
				{
					nInvertedCount++;
				}
			}
		}

		mTagString.nInvertedCount = nInvertedCount;
		if ( mStrList.empty() )
		{
			mStrList.push_back( mTagString );
		}
		else
		{
			int i = mStrList.size()-1;
			for ( ; i >= 0; --i )
			{
				if ( mStrList[i].nInvertedCount > nInvertedCount )
				{
					continue;
				}
				break;
			}
			mStrList.insert( mStrList.begin() + (i + 1), mTagString );
		}

		--m;
	}

	for ( deque<tagString>::size_type i = 0; i < mStrList.size(); ++i )
	{
		cout << mStrList[i].strSequence << endl;
	}

	return 0;
}

归并排序找逆序对(时间复杂度为O(nlogn))

#include <iostream>
#include <string>
#include <deque>
#include <cmath>

using namespace std;

int Merge( string& strSrc, int low, int mid, int high )
{
	int i = low;
	int j = mid + 1;
	int nCount = 0;
	string strTemp( high - low + 1, 0 );
	int k = 0;

	while( i <= mid && j <= high )
	{
		if ( strSrc[i] <= strSrc[j] )
		{
			strTemp[k++] = strSrc[i++];
		}
		else
		{
			strTemp[k++] = strSrc[j++];
			nCount += j - k - low;
		}
	}

	while( i <= mid )
	{
		strTemp[k++] = strSrc[i++];
	}
	while( j <= high )
	{
		strTemp[k++] = strSrc[j++];
	}

	for ( k = 0; k < (high-low+1); ++k )
	{
		strSrc[low+k] = strTemp[k];
	}

	return nCount;
}

int MergeSort( string& strSrc, int low, int high )
{
	int nCount = 0;
	if ( low < high )
	{
		int mid = ( low + high ) / 2;
		nCount += MergeSort( strSrc, low, mid );
		nCount += MergeSort( strSrc, mid + 1, high );
		nCount += Merge( strSrc, low, mid, high );
	}

	return nCount;
}

struct tagString
{
	int nInvertedCount;
	string strSequence;
};

int main()
{
	deque<tagString> mStrList;
	tagString mTagString;
	short n,m;
	cin >> n >> m;
	while( m > 0 )
	{
		cin >> mTagString.strSequence;
		string strTemp = mTagString.strSequence;
		mTagString.nInvertedCount = MergeSort( strTemp, 0, strTemp.size()-1 );
		if ( mStrList.empty() )
		{
			mStrList.push_back( mTagString );
		}
		else
		{
			int i = mStrList.size()-1;
			for ( ; i >= 0; --i )
			{
				if ( mStrList[i].nInvertedCount > mTagString.nInvertedCount )
				{
					continue;
				}
				break;
			}
			mStrList.insert( mStrList.begin() + (i + 1), mTagString );
		}

		--m;
	}

	for ( deque<tagString>::size_type i = 0; i < mStrList.size(); ++i )
	{
		cout << mStrList[i].strSequence << endl;
	}

	return 0;
}

总结

理论上来说,第二种方法要快一些,但是第二种方法里在排序的时候存在很多的交换,在OJ上编译运行反而是第一种方法更快。

作者:山丘儿

转载请标明出处,谢谢。原文地址:http://blog.csdn.net/s634772208/article/details/46568015

时间: 2024-10-12 17:20:23

北大OJ_1007题:DNA Sorting的相关文章

北大ACM题库习题分类与简介(转载)

在百度文库上找到的,不知是哪位大牛整理的,真的很不错! zz题 目分类 Posted by fishhead at 2007-01-13 12:44:58.0 -------------------------------------------------------------------------------- acm.pku.edu.cn 1. 排序 1423, 1694, 1723, 1727, 1763, 1788, 1828, 1838, 1840, 2201, 2376, 23

poj 1007 DNA Sorting (求逆序数)

DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 83069   Accepted: 33428 Description 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 instanc

算法:POJ1007 DNA sorting

这题比较简单,重点应该在如何减少循环次数. package practice; import java.io.BufferedInputStream; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; /** * DNA sorting * * @author caiyu * @date 2014-11-5 */ public class POJ1007 { public static void m

DNA Sorting(排序)

欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) DNA Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2182    Accepted Submission(s): 1062 Problem Description One measure of ``unsortedness'' in a sequenc

HDU 1379 DNA sorting(求逆序数)

DNA Sorting Problem Description 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 t

北大POJ题库使用指南

原文地址:北大POJ题库使用指南 北大ACM题分类主流算法: 1.搜索 //回溯 2.DP(动态规划)//记忆化搜索 3.贪心 4.图论 //最短路径.最小生成树.网络流 5.数论 //组合数学(排列组合).递推关系.质因数法 6.计算几何 //凸壳.同等安置矩形的并的面积与周长.凸包计算问题 8.模拟 9.数据结构 //并查集.堆.树形结构 10.博弈论 11.CD有正气法题目分类: 1. 排序 1423, 1694, 1723, 1727, 1763, 1788, 1828, 1838, 1

hdoj 1379 DNA Sorting

DNA Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2231    Accepted Submission(s): 1096 Problem Description One measure of ``unsortedness'' in a sequence is the number of pairs of entr

zju 1188 DNA Sorting

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct node { int a; char s[105]; }p[105]; int cmp(node c,node b) { return c.a<b.a; } int main() { int m,n; int t; scanf("%d",&t); //printf(&q

hdu 1379 DNA Sorting

DNA Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1924    Accepted Submission(s): 949 Problem Description One measure of ``unsortedness'' in a sequence is the number of pairs of entrie