anagramrolf.c

来自「1995年ACM contest FINAL试题和源码」· C语言 代码 · 共 79 行

C
79
字号
/*1995-96 ACM International Collegiate Programming ContestSouthwestern European Regional ContestETH Zurich, SwitzerlandDecember 9, 1995Problem: AnagramIdea:			Rolf Strebel, ETH ZurichImplementation:	Manuel Bleichenbacher, Head JudgeSource file: anagram.c / anagram.pInput file: anagram.inOutput file: anagram.out*/#include <stdio.h>#include <string.h>#include <ctype.h>#define BUFSIZE 4096int no, nofchars;char chars[256];int cnt[256];int incnt[256];char line[BUFSIZE+1];char pat[BUFSIZE+1];FILE *fin, *fout;void permute (int pos){  int k;  if (pos == nofchars) {    pat[pos] = '\0'; fprintf (fout, "%s\n", pat);  } else {    for (k = 0; k < no; k++) {      if (cnt[k] > 0) {       pat[pos] = chars[k];        cnt[k]--; permute (pos+1); cnt[k]++;      }    }  }}  int main (int argc, char **argv){  int i, no_in;  char *str;  if ((fin = fopen ("anagram.in","r")) == NULL) exit (1);  if ((fout = fopen ("anagram.out","w")) == NULL) exit (1);    fscanf (fin, "%i", &no_in);  while (no_in-- > 0) {    for (i = 0; i < 256; i++) incnt[i] = 0;    fscanf (fin, "%s", line);    for (str = line; isalpha(*str); str++) {      incnt[(int)*str] += 1;    }    for (no = 0, nofchars = 0, i = 0; i < 256; i++) {      if (incnt[i] > 0) {       chars[no] = i; cnt[no] = incnt[i]; no++;       nofchars += incnt[i];      }    }    permute (0);  }  fclose (fout);  fclose (fin);  return (0);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?