0% found this document useful (0 votes)
355 views1 page

Anagrame Backtracking

This C code uses backtracking to generate and output all possible anagrams of a given input word. It takes a word as input, stores it in an array, and uses a recursive backtracking function to try all permutations of the indexes of the input array, checking each one for validity before outputting valid anagrams to a file. The main function gets the input word, calls the backtracking function starting at index 1, and returns 0 when complete.

Uploaded by

Stefan Idriceanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
355 views1 page

Anagrame Backtracking

This C code uses backtracking to generate and output all possible anagrams of a given input word. It takes a word as input, stores it in an array, and uses a recursive backtracking function to try all permutations of the indexes of the input array, checking each one for validity before outputting valid anagrams to a file. The main function gets the input word, calls the backtracking function starting at index 1, and returns 0 when complete.

Uploaded by

Stefan Idriceanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

//anagrame backtracking

#include<stdio.h>
#include<string.h>
int st[100],n;
char cuv[100];
FILE*f=fopen("date.in","r");
FILE*g=fopen("date.out","w");
int valid(int k)
{ for(int i=1;i<k;i++)
if(st[k]==st[i])
return 0;
return 1;
}
void back(int k)
{if(k==n+1)
{for(int i=1;i<=n;i++)
fprintf(g,"%c",cuv[st[i]]);
fprintf(g,"\n");}
else for(int i=0;i<n;i++)
{st[k]=i;
if(valid(k))
back(k+1);}}
int main()
{fscanf(f,"%s",&cuv);
n=strlen(cuv);
back(1);
return(0);
}

You might also like