1) one to get a digit at some position
2) one to compare 2 integers (this was the more dificult)
3) I used in the main function an array, that was sorted by insertion (read a number and insert it into the array)
![:)](./images/smilies/icon_smile.gif)
Moderator: Board moderators
Code: Select all
#include <iostream>
#include <cstring>
#include <queue>
#include <memory>
#include <cstdlib>
#include <math.h>
using namespace std ;
char input [50][3000] ;
char result [1000000] ;
int order [50] ;
char t [1000000] ;
int N ;
bool generateString ()
{
t [0] = NULL ;
for ( int i=0 ; i<N ; i++ )
strcat ( t , input [order [i]] ) ;
if ( strcmp ( t , result ) == 1 ) // if the t is bigger
{
strcpy ( result , t ) ;
return true ;
}
return false ;
}
int main ()
{
while ( cin >> N && N )
{
cin.get () ;
result [0] = NULL;
long long X ;
for ( int i=0 ; i<N ; i++ )
{
while ( cin.peek () == '0' || cin.peek () == '\n' ) cin.get () ;
int j ;
for ( j=0 ; cin.peek () >= '0' && cin.peek ()<= '9' ; j++ )
input [i][j] = cin.get () ;
input [i][j] = NULL ;
cin.get ();
}
for ( int i=0 ; i<N ; i++ )
order [i] = i ;
generateString () ;
bool chg ;
chg = 0 ;
while ( 1 )
{
chg = 0 ;
for ( int i=0 ; i<N && !chg; i++ )
{
if ( i == ( N-1 ) ) goto Final ;
for ( int j=(i+1) ; j<N && !chg; j++ )
{
swap ( order [i] , order [j] ) ;
if ( !generateString () )// if less
swap ( order [i] , order [j] ) ;
else // if greater
chg = 1;
}
}
if ( !chg ) goto Final ;
}
Final :
int I ;
for ( I=0 ; I<strlen ( result ) && result [I] == 0 ; I++ ); // skip leading zeros
for ( ; I<strlen ( result ) ; I++ ) cout << result [I] ;
cout << endl ;
}
}
You write that your solution is using DP... but it's definitely not DP what you've posted here... your algorithm looks a litle bit strange and rather slow, but actually I've not found any counter example for it, yet...arsalan_mousavian wrote:yeah , i am getting WA under 1 sec
toyour code wrote:...if ( strcmp ( t , result ) == 1 ) // if the t is bigger
as man strcmp saysnew code wrote:...if ( strcmp ( t , result ) > 0 ) // if the t is bigger
man strcmp wrote:The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.
Your solution's not working forsakhassan wrote:I have tried all the possible input.... But still getting WA... I dont know what i have missed!!!!!!!!!!!!! pls help me![]()
Code: Select all
2
47107575102712769936 68510897482010786149041631210810410110497141076106261021082276567248265259910827465
0
Code: Select all
6851089748201078614904163121081041011049714107610626102108227656724826525991082746547107575102712769936
Btw, there is already a thread on this problem. If there is a thread on a particular problem, please, use it to post your question and do not create a new one. (see http://online-judge.uva.es/board/viewtopic.php?t=9498, http://online-judge.uva.es/board/viewtopic.php?t=9006 and http://online-judge.uva.es/board/viewtopic.php?t=8983)sakhassan wrote:I have tried all the possible input.... But still getting WA... I dont know what i have missed!!!!!!!!!!!!! pls help me
Your new solution (with the modified comparison function) does not work for:sakhassan wrote:I have modified my comparison function like this:
can any1 have any idea???
Code: Select all
10
524247331010 7 10078 129043310 63915138 5 7106 10213109 2145 7626644593
0
Code: Select all
77626644593710663915138552424733101021451290433101021310910078
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
//WA...
using namespace std;
int max(int a,int b) {
return a>b?a:b;
}
typedef struct {
string si;
int len;
int idx;
} T;
T t[100];
int compare(const void *a,const void *b) {
T* x = (T*) a;
T* y = (T*) b;
int count1 = 0;
int count2 = 0;
int ll = max(x->len,y->len);
for (int i=0;i<ll;i++) {
if (count1 >= x->len) count1 = 0;
if (count2 >= y->len) count2 = 0;
int tmp1 = (x->si[count1])-'0';
int tmp2 = (y->si[count2])-'0';
if (tmp1 != tmp2) return y->si[count2] - x->si[count1];
count1++; count2++;
}
return x->len - y->len;
}
int cmp(const void *a,const void *b) {
T* x = (T*) a;
T* y = (T*) b;
int count1 = 0;
int count2 = 0;
int ll = max(x->len,y->len);
for (int i=0;i<ll;i++) {
if (count1 >= x->len) count1 = 0;
if (count2 >= y->len) count2 = 0;
int tmp1 = (x->si[count1])-'0';
int tmp2 = (y->si[count2])-'0';
if (tmp1 != tmp2) return y->si[count2] - x->si[count1];
count1++; count2++;
}
return y->len - x->len;
}
int main () {
int n,tt;
int count = 0;
char s[10000];
char *tokenptr;
string ret1 = "";
string ret2 = "";
while (scanf("%d",&n) != EOF) {
ret1 = "";
ret2 = "";
count = 0;
if (!n) return 0;
for(int i=0;i<n;i++) {
scanf("%s",s);
//sprintf(s,"%d",tt);
t[count].si = s;
t[count].len = strlen(s);
count++;
}
/*tokenptr = strtok(s," ");
while (tokenptr != NULL) {
//t[count].idx = count;
//strcpy(t[count].si,tokenptr);
t[count].si = tokenptr;
t[count].len = strlen(tokenptr);
count++;
tokenptr = strtok(NULL," ");
}*/
int temp = 0;
temp=count+1;
qsort(t,temp,sizeof(T),compare);
for (int i=0;i<temp;i++) {
ret1+=t[i].si;
}
qsort(t,temp,sizeof(T),cmp);
for (int i=0;i<temp;i++) {
ret2+=t[i].si;
}
if (ret1 > ret2) cout << ret1;
else cout << ret2;
printf("\n");
//cout << ret1 << "\n" << ret2;
//printf("\n");*/
//cout << ret1 << "\n";
for (int i=0;i<temp;i++) {
t[i].si = "";
}
}
return 0;
}
Code: Select all
5
1 120 1201 12012 120120
5
7 768 7687 76876 768768
0
Code: Select all
1201212012012012011
7768776876876876876