
and what was the problem,Roby?
Moderator: Board moderators
Code: Select all
int comparer( const void * a, const void * b )
{
char * va = ( char * ) a;
char * vb = ( char * ) b;
char bigA, bigB;
bigA = islower(*va) ? (*va)-32 : (*va);
bigB = islower(*vb) ? (*vb)-32 : (*vb);
if ( bigA != bigB )
return bigA-bigB;
else
return (*va)-(*vb);
}
Code: Select all
1
AaB
Code: Select all
AaB
ABa
aAB
aBA
BAa
BaA
Code: Select all
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SIZE 10000
void swap( char *x, char *y )
{
char tmp = *x;
*x = *y;
*y = tmp;
}
int ch_cmp( const void *a, const void *b )
{
return *((char *)a) - *((char *)b);
}
int fact( int n )
{
return (n == 1) ? 1 : n*fact(n-1);
}
void next_permutation( char *seq, int last )
{
int j, k, r, s;
j = last-1;
while( seq[j] > seq[j+1] ) j--;
k = last;
while( seq[j] > seq[k] ) k--;
swap( &seq[j], &seq[k] );
r = last;
s = j + 1;
while( r > s )
{
swap( &seq[r], &seq[s] );
r--;
s++;
}
}
int main()
{
char seq[MAX_SIZE] = {0};
int slen, np, ntest;
scanf( "%d", &ntest );
while( ntest-- )
{
scanf( "%s", seq );
slen = strlen(seq);
qsort( seq, slen, sizeof(char), ch_cmp );
np = fact(slen)-1;
printf( "%s\n", seq );
while( np-- )
{
next_permutation( seq, slen-1 );
printf( "%s\n", seq );
}
}
return 0;
}
Code: Select all
05565437_24.c: In function `prenditesto':
05565437_24.c:53: parse error before `char'
05565437_24.c:56: `arg' undeclared (first use in this function)
05565437_24.c:56: (Each undeclared identifier is reported only once
05565437_24.c:56: for each function it appears in.)
05565437_24.c: In function `purge':
05565437_24.c:68: parse error before `int'
05565437_24.c:71: `i' undeclared (first use in this function)
05565437_24.c:73: `res' undeclared (first use in this function)
Code: Select all
#include <stdio.h>
#include <string.h>
int len=0;
void purge(char *arr);
void prenditesto();
void permuta(char *parola, int n);
void stampa(char *parola);
int main()
{
prenditesto();
return 0;
}
void permuta(char *parola, int n)
{
if(n==len-1)
stampa(parola);
else
{
int i;
for(i=n;i<len;i++)
{
int temp = parola[i];
parola[i] = parola[n];
parola[n] = temp;
permuta(parola,n+1);
temp = parola[i];
parola[i] = parola[n];
parola[n] = temp;
}
}
}
void stampa(char *parola)
{
int i;
for(i=0;parola[i]!='\0';i++)
{
printf("%c", parola[i]);
}
printf("%c", '\n');
}
void prenditesto()
{
int num;
int i;
scanf("%d", &num);
char *arg[num];
for(i=0;i<num;i++)
{
scanf("%s", arg[i]);
}
for(i=0;i<num;i++)
{
purge(arg[i]);
permuta(arg[i],0);
}
}
void purge(char *arr)
{
len=strlen(arr);
int i;
char res[len];
for(i=0;i<len;i++)
{
res[i]=arr[i];
}
arr=res;
}
Code: Select all
char *arg[num];
Code: Select all
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int charmap[256];
void print_str(char*);
class node {
private:
char data[100];
node *next;
public:
friend class linklist;
};
class linklist {
private:
node *first;
public:
linklist():first(0){}
void insert(char *);
void destroy();
void display();
};
void linklist::insert(char *str) {
node *tmp;
tmp=new node();
strcpy(tmp->data,str);
tmp->next=first;
if(first==0) {
first=tmp;
return;
}
if(strcmp(first->data,tmp->data)==0) return;
if(strcmp(first->data,tmp->data)>0) {
tmp->next=first;
first=tmp;
return;
}
node *curr=first;
while(curr->next && strcmp(curr->next->data,tmp->data)<0) {
curr=curr->next;
}
if(curr->next && strcmp(curr->next->data,tmp->data)==0) return;
if(strcmp(curr->data,tmp->data)==0) return;
tmp->next=curr->next;
curr->next=tmp;
}
void linklist::display() {
for(node *curr=first;curr;curr=curr->next)
print_str(curr->data);
}
void linklist::destroy() {
node *curr=first,*next;
while(curr) {
next=curr->next;
delete curr;
curr=next;
}
first=0;
}
void swap(char *str, int x, int y) {
char tmp=str[x];
str[x]=str[y];
str[y]=tmp;
}
void print_str(char *str) {
int len=strlen(str);
for(int i=0;i<len;i++)
for(int j=0;j<charmap[str[i]];j++)
cout<<str[i];
cout<<endl;
}
int comp_char(const void *a, const void *b) {
char *p=(char*)a, *q=(char*)b;
return *p-*q;
}
void HeapPermute(char *str, int n, linklist& l)
{
if (n == 1)
l.insert(str);
else {
for (int i = 0; i < n; i++) {
HeapPermute(str, n-1,l);
if (n % 2 == 1) // if n is odd
swap(str, 0, n-1);
else // if n is even
swap(str, i, n-1);
}
}
}
int main() {
int num;
char word[20][100];
linklist l;
// Get the Number of Words
cin>>num;
for(int i=0;i<num;i++){
cin>>word[i];
}
// Process & Display Anagram for each word
for(int i=0;i<num;i++) {
int len=strlen(word[i]);
for(int j=0;j<256;j++) charmap[j]=0;
for(int j=0;j<len;j++) charmap[word[i][j]]++;
qsort((void*)word[i],len,1,comp_char);
// Shrink word[i];
int r,w=0;
for(r=1;r<len;r++){
if(word[i][w]==word[i][r]) {
} else {
word[i][++w]=word[i][r];
}
}
word[i][++w]=0;
len=strlen(word[i]);
HeapPermute(word[i],len,l);
l.display();
l.destroy();
}
return 0;
}
No it won'tshamim wrote:You can not declare array like this. num must be a constant, or you need to allocate memory dynamically by using malloc().Code: Select all
char *arg[num];
The code will however compile if you specify C++ as the language.
Code: Select all
code removed