612 - DNA Sorting

All about problems in Volume 6. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Maddas
New poster
Posts: 11
Joined: Sat Apr 03, 2004 1:45 am

Post by Maddas » Wed Apr 28, 2004 12:53 am

Aaaargh, I confused maximum string length and maximum number of sequences in my code. The problem just didn't show for my input

Thanks a lot :-)

wanderley2k
New poster
Posts: 28
Joined: Mon Mar 01, 2004 11:29 pm

Post by wanderley2k » Tue May 11, 2004 12:52 am

Where is mistake in my code? :roll:

[cpp]#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

typedef struct DNA_s DNA;
struct DNA_s
{
string seq;
int inversions;
};


bool operator<(const DNA& x, const DNA& y)
{
return x.inversions < y.inversions;
}

int value(char c) {
if (c == 'A') return 0;
if (c == 'C') return 1;
if (c == 'G') return 2;
if (c == 'T') return 3;
}

int main()
{
int inst, n, m, i, w, z, sumInversions;
string takeRead;

cin >> inst;

for (; inst > 0 ; --inst)
{
cin >> n >> m;
vector<DNA> set;
for (i = 0; i < m; i++) {
DNA tmp;
cin >> tmp.seq;

int count[4] = { 0, 0, 0, 0 };
sumInversions = 0;
for (w = 0; w < tmp.seq.size(); ++w)
{
count[value(tmp.seq[w])]++;
for (z = value(tmp.seq[w])+1; z < 4; ++z) { sumInversions += count[z]; }

/*
*if (tmp.seq[w] == 'A') { continue; }
*for (z = w; z < tmp.seq.size(); ++z)
*{
* if (value(tmp.seq[w]) > value(tmp.seq[z]))
* {
* sumInversions++;
* }
*}
*/
}
tmp.inversions = sumInversions;
set.push_back(tmp);
}

sort(set.begin(), set.end());

for (i = 0; i < set.size(); ++i) {
cout << set.seq << '\n';
}
if (inst-1 > 0) { cout << '\n'; }
}

return 0;
}
[/cpp]

wanderley2k
New poster
Posts: 28
Joined: Mon Mar 01, 2004 11:29 pm

612 - Why wrong??

Post by wanderley2k » Tue May 11, 2004 12:54 am

Where is mistake in my code? I tested with diverses input data and all answer is correct...

my code:
[cpp]#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

typedef struct DNA_s DNA;
struct DNA_s
{
string seq;
int inversions;
};


bool operator<(const DNA& x, const DNA& y)
{
return x.inversions < y.inversions;
}

int value(char c) {
if (c == 'A') return 0;
if (c == 'C') return 1;
if (c == 'G') return 2;
if (c == 'T') return 3;
}

int main()
{
int inst, n, m, i, w, z, sumInversions;
string takeRead;

cin >> inst;

for (; inst > 0 ; --inst)
{
cin >> n >> m;
vector<DNA> set;
for (i = 0; i < m; i++) {
DNA tmp;
cin >> tmp.seq;

int count[4] = { 0, 0, 0, 0 };
sumInversions = 0;
for (w = 0; w < tmp.seq.size(); ++w)
{
count[value(tmp.seq[w])]++;
for (z = value(tmp.seq[w])+1; z < 4; ++z) { sumInversions += count[z]; }

/*
*if (tmp.seq[w] == 'A') { continue; }
*for (z = w; z < tmp.seq.size(); ++z)
*{
* if (value(tmp.seq[w]) > value(tmp.seq[z]))
* {
* sumInversions++;
* }
*}
*/
}
tmp.inversions = sumInversions;
set.push_back(tmp);
}

sort(set.begin(), set.end());

for (i = 0; i < set.size(); ++i) {
cout << set.seq << '\n';
}
if (inst-1 > 0) { cout << '\n'; }
}

return 0;
}
[/cpp]

wanderley2k
New poster
Posts: 28
Joined: Mon Mar 01, 2004 11:29 pm

Post by wanderley2k » Thu May 13, 2004 5:40 pm

I got AC with one tick!!!

[cpp]stable_sort(set.begin(), set.end());[/cpp]

it is correct!!

Thanks Gustavo and JP

Wanderley 8)

** ALIVE THE SOCIALISM REVOLUTION **

Minilek
Learning poster
Posts: 90
Joined: Tue Jul 27, 2004 9:34 am
Location: Cambridge, MA
Contact:

Post by Minilek » Sun Aug 08, 2004 3:52 pm

[EDIT]
I am dumb. I didn't realize this was a multiple input problem..

grrr...5 WA's for nothing!

AC now 8)

While we're on the subject... :D
Does anyone have some speed tips for this problem?
I use modified counting sort to count inversions in O(n),
and before I qsort the arrays based on inversion count,
I memoize the inversion count for each string so that qsort
can just look it up in the array versus calling count_inversion()
multiple times for each string. This got me only down to .271s
but apparently there seem to be faster ways..anyone know
any ?
[/EDIT]

Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

612 Why WA???

Post by Morning » Sat Oct 02, 2004 3:09 pm

[cpp]
#include <iostream>
#include <cstring>
using namespace std;
int count(char str[51],int n)
{
int sum = 0;
for(int i = 0;i < n;i++)
{
for(int j = i + 1;j < n;j++)
{
if(str > str[j]){sum++;}
}
}
return sum;
}
int main()
{
int n,m,store[100],i,j,t;
char str[100][51],temp[51];


while(cin >> n >> m)
{
for(i = 0;i < m;i++)
{
cin >> str;
store = count(str,51);
}
//bubble sort
for(i = 0;i < m - 1;i++)
{
for(j = 0;j < m - i - 1;j++)
{
if(store[j] > store[j + 1])
{
t = store[j + 1];
store[j + 1] = store[j];
store[j] = t;
strcpy(temp,str[j + 1]);
strcpy(str[j + 1],str[j]);
strcpy(str[j],temp);
}
}
}
for(i = 0;i < m;i++){cout << str << endl;}
}
return 0;
}
[/cpp]
Last edited by Morning on Sun Oct 03, 2004 5:35 pm, edited 1 time in total.
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius

User avatar
dumb dan
Learning poster
Posts: 67
Joined: Tue Aug 05, 2003 1:02 am

Post by dumb dan » Sun Oct 03, 2004 4:59 pm

This is a multiple input problem!

Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

Post by Morning » Sun Oct 03, 2004 5:32 pm

i was totally confused.
The first line of a multiple input file is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
I didn't see a integer N in the first line in the Sample input,but i still change my code to
[cpp]
#include <iostream>
#include <cstring>
using namespace std;
int count(char str[51],int n)
{
int sum = 0;
for(int i = 0;i < n;i++)
{
for(int j = i + 1;j < n;j++)
{
if(str > str[j]){sum++;}
}
}
return sum;
}
int main()
{
int N,n,m,store[100],i,j,t;
char str[100][51],temp[51];

cin >> N;
while(N--)
{
cin >> n >> m;
for(i = 0;i < m;i++)
{
cin >> str;
store = count(str,51);
}
//bubble sort
for(i = 0;i < m - 1;i++)
{
for(j = 0;j < m - i - 1;j++)
{
if(store[j] > store[j + 1])
{
t = store[j + 1];
store[j + 1] = store[j];
store[j] = t;
strcpy(temp,str[j + 1]);
strcpy(str[j + 1],str[j]);
strcpy(str[j],temp);
}
}
}
for(i = 0;i < m;i++){cout << str << endl;}
}
return 0;
}
[/cpp]
still got WA,pliz,why??
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius

User avatar
dumb dan
Learning poster
Posts: 67
Joined: Tue Aug 05, 2003 1:02 am

Post by dumb dan » Sun Oct 03, 2004 6:08 pm

store = count(str,51);

I think you want to change that to

store = count(str,n);

Besides that you'll get a PE if you don't seperate each case with a blank line.

Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

Post by Morning » Sun Oct 03, 2004 6:18 pm

dumb dan,thanx :oops:
u always help me
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius

hsuyt831
New poster
Posts: 3
Joined: Mon Dec 16, 2002 4:53 am

612

Post by hsuyt831 » Fri Oct 15, 2004 11:05 am

i use merge sort to solve this problem
but i can't figure out why i get WA
could someone help me ??

[c]
#include <stdio.h>
#include <stdlib.h>

int count;

struct measure{
int ans;
int num;
};
typedef struct measure Measure;
void mergeSort(char[],int, int);
void merge(char[],int, int, int);
void sort(Measure[], int);



int main(){

int m,n;
int k,i,j;
char input[1000][1000], A[1000];
Measure result[1000];

scanf("%d %d\n",&n,&m);
for(i=0;i<m;i++){
scanf("%s",&input);
}

for(i=0;i<m;i++)
result.num=i;

for(i=0;i<m;i++){
for(j=0;j<n;j++)
A[j]=input[j];
count=0;
mergeSort(A,0,n-1);
result.ans=count;
}

sort(result,m);
for(i=0;i<m;i++){
k=result.num;
for(j=0;j<n;j++)
printf("%c",input[k][j]);
printf("\n");
}
return 0;
}

void mergeSort(char A[],int p,int r){

int q;

if(p<r){
q=(p+r)/2;
mergeSort(A,p,q);
mergeSort(A,q+1,r);
merge(A,p,q,r);
}
}

void merge(char A[],int p,int q, int r){

int i,j,k;
int n1,n2;
char L[501],R[501];

n1=q-p+1;
n2=r-q;

for(i=0;i<n1;i++)
L=A[p+i];
for(j=0;j<n2;j++)
R[j]=A[q+1+j];
L[n1]='Z';
R[n2]='Z';
i=0;
j=0;
for(k=p;k<=r;k++){
if(L<=R[j]){
A[k]=L;
i++;
}
else{
A[k]=R[j];
j++;
count=count+(n1-i);
}
}
}

void sort(Measure A[], int size){

int i,pass;
Measure hold;

for(pass=1;pass<size;pass++)
for(i=0;i<size-1;i++)
if(A.ans > A[i+1].ans){
hold = A;
A[i] = A[i+1];
A[i+1] = hold;
}
}
[/c]

Heartattack!
New poster
Posts: 45
Joined: Fri Jan 16, 2004 7:02 pm
Location: CSE::BUET
Contact:

612 DNA Sorting: RTE: invalid memory ref?!?HELP!!!!

Post by Heartattack! » Mon Nov 08, 2004 5:32 pm

My code works perfectly on my machine. I compiled it with vc++.net. Running on the judge it gave a run time error: "invalid memory reference". Could someone please tell me what's wrong? The algo is simple. I get the input, check for the sortedness through the bubble sort check, qsort the data and print it out.....only not on the judge :oops: :oops: :oops: :oops:
Please help. Thanks in advance:
[cpp]

// p612.DNA Sorting.cpp : Defines the entry point for the console application.
//

@begin_of_source_code

/* @JUDGE_ID: ****** 612 C++ */

#include "stdio.h"


struct str
{
int Srtdnes;
char seq[51];
};

int Cmp(const void *x,const void *y)
{
int a=((str*)x)->Srtdnes;
int b=((str*)y)->Srtdnes;
return a-b;
}

void main(void)
{
int length,lines;

scanf("%d%d",&length,&lines);


str *a=(str*)malloc(sizeof(str)*lines);

for(int i=0;i<lines;i++)
{
scanf("%s",a.seq);
a.Srtdnes=0;
for(int j=1;j<length;j++)
for(int k=0;k<j;k++)
if(a.seq[j]<a.seq[k])
a.Srtdnes++;
}
qsort(a,lines,sizeof(str),Cmp);

for(int i=0;i<lines;i++)
puts(a.seq);



}


@end_of_source_code

[/cpp]
We will, We will BREAK LOOP!!!!

User avatar
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 » Tue Nov 09, 2004 2:46 am

It's a multiple input problem (the blue checkmark next to the problem's name in the volume's index).

http://online-judge.uva.es/problemset/minput.html

Heartattack!
New poster
Posts: 45
Joined: Fri Jan 16, 2004 7:02 pm
Location: CSE::BUET
Contact:

Post by Heartattack! » Wed Nov 10, 2004 7:21 am

Thanks, I'll try it with multiple input. It was really silly of me to miss the blue tick.... :oops: :oops:
We will, We will BREAK LOOP!!!!

Raiyan Kamal
Experienced poster
Posts: 106
Joined: Thu Jan 29, 2004 12:07 pm
Location: Bangladesh
Contact:

Post by Raiyan Kamal » Wed Dec 29, 2004 8:13 pm

are you sure you are taking care of the ' multiple input ' - thing properly ?

Post Reply

Return to “Volume 6 (600-699)”