Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

c++ 除了快排,另一种方法也是0ms,弱弱纪念下

Posted by liyan199311 at 2017-02-20 16:13:15 on Problem 1007
代码可能有很多能优化的地方,主要是思路:
ACGT三次循环,第一次循环,将所有的A移动到最前面的位置,其他类似,值得注意当ACG排好就不用排T了,此外每次移动查看后面的字符与当前是否一样,可以减少循环总次数,想法和快排一样。PS:0ms



#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<math.h>
#include<algorithm>
using namespace std;

int getIndex(const char a)
{
	switch (a)
	{
	case 'A':
		return 0;
	case 'C':
		return 1;
	case 'G':
		return 2;
	case 'T':
		return 3;
	}
}

int getInt(const string s)
{
	static string acgt_array[4] = { "A", "C", "G", "T" };
	int size_array[4] = {}, index_array[4] = {};
	string s_copy(s);
	int result = 0;
	for (string::iterator i = s_copy.begin(); i != s_copy.end(); i++)
	{
		size_array[getIndex(*i)] ++;
	}
	//计算
	for (int j = 0; j < 3; j++)
	{
		string temp_copy = "";
		for (int i = 0; i < s_copy.length();)
		{
			int sum = 0, temp_result = 0;
			while (i < s_copy.length() && getIndex(s_copy[i]) == j)
			{
				if (sum == 0)
				{
					temp_result = i - index_array[j];
				}
				i++;
				sum++;
				index_array[j] ++;
			}
			if (sum)
			{
				result += sum* temp_result;
			}
			else
			{
				temp_copy += s_copy[i];
				i++;
			}
		}
		s_copy = temp_copy;
	}
	return result;
}

int main()
{
	int length, size;
	cin >> length >> size;
	map<int, string> map_data;
	
	string temp = "";
	int index = 0;
	while (index < size)
	{
		cin >> temp;
		index++;
		int sum = getInt(temp);
		if (map_data[sum] == "")
		{
			map_data[sum] = temp;
		}
		else
		{
			map_data[sum] += temp;
		}
	}
	for (map<int, string>::iterator it = map_data.begin(); it != map_data.end(); it++)
	{
		temp = (*it).second;
		int length_temp = temp.length();
		for (int i = 0; i < length_temp / length; i++)
		{
			string mm = string(temp.begin() + i * length, temp.begin() + (i + 1) * length);
				cout << mm << endl;
		}
	}
	system("pause");
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator