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

Re:Hi guys, could you please view my code and tell me why I was wrong?

Posted by lsjseu at 2013-01-20 00:08:45 on Problem 1007
In Reply To:Hi guys, could you please view my code and tell me why I was wrong? Posted by:ducfilan at 2013-01-01 17:06:02
> This is my code:
> 
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int merge(string& a,int begin,int mid,int end)
{
	int reverse_count = 0;
	int i = begin,k = begin;
	int j = mid+1;
	string temp(end+1,' ');
	
    while(i<=mid&&j<=end){
		if(a[i]<=a[j])
			temp[k++]=a[i++];
		else{
			temp[k++]=a[j++];
			reverse_count += (mid-i+1);
		}
	}
	
	while(i<=mid)temp[k++]=a[i++];
	while(j<=end)temp[k++]=a[j++];
	
    for(i=begin;i<=end;++i){
		a[i] = temp[i];
	}

	return reverse_count;
}

int merge_sort(string &str,int begin,int end)
{
	int count = 0;
	if(begin<end)
	{
		int mid = (begin+end)/2;
		count += merge_sort(str,begin,mid);
		count += merge_sort(str,mid+1,end);
		count += merge(str,begin,mid,end);
	}
	return count;
}

struct node{
	
	/*friend ostream& operator<<(ostream& os,const node& n)
	{
		os<<"<"<<n.str<<","<<n.count<<">";
		return os;
	}*/

	string str;
	int count;
	node(string s="",int _count=0):str(s),count(_count){};
};


bool cmp(node a,node b)
{
	return a.count<b.count;
}

void insertsort(node a[],int begin,int end)
{
	int j;
	for(int i=begin+1;i<end;++i)
	{
		node temp = a[i];
		j = i-1;
		while(j>=0&&a[j].count>temp.count)
		{
			a[j+1] = a[j];
			j--;
		}
		a[j+1] = temp;
	}
}

int main()
{
    int n,m,i=0;
	cin>>n>>m;
	string str,tmp;
	node *temp = new node[m];
	while(i<m){
		cin>>str;
		tmp = str;
		temp[i] = node(tmp,merge_sort(str,0,n-1));
		i++;
	}
  
	sort(temp,temp+m,cmp);

	for(i=0;i<m;++i){
		cout<<temp[i].str<<endl;
	}

	delete []temp;
	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