| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:Hi guys, could you please view my code and tell me why I was wrong?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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator