博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVa 195-Anagram
阅读量:6334 次
发布时间:2019-06-22

本文共 2690 字,大约阅读时间需要 8 分钟。

hot3.png

问题描述】

    You are to write a program that has to generate all possible words from a given set of letters. Example: Given the word "abc", your program should - by exploring all different combination of the three letters - output the words "abc", "acb", "bac", "bca", "cab" and "cba". In the word taken from the input file, some letters may appear more than once. For a given word, your program should not produce the same word more than once, and the words should be output in alphabetically ascending order.

Input

    The input file consists of several words. The first line contains a number giving the number of words to follow. Each following line contains one word. A word consists of uppercase or lowercase letters from A to Z. Uppercase and lowercase letters are to be considered different.

Output

    For each word in the input file, the output file should contain all different words that can be generated with the letters of the given word. The words generated from the same input word should be output in alphabetically ascending order. An upper case letter goes before the corresponding lower case letter.

Sample Input 24232413_WG22.gif

3aAbabcacba

Sample Output

AabAbaaAbabAbAabaAabcacbbacbcacabcbaaabcaacbabacabcaacabacbabaacbacabcaacaabcabacbaa

【解题思路

    简单题、搜索。递归。

【具体实现

#include 
  #include 
  #include 
    char data[ 200 ];  bool used[ 200 ];  char last[ 200 ];  char save[ 200 ];    int low( char c )  {      if ( c >= 'a' && c <= 'z' )          return c;      return c + 'a'-'A';  }    int cmp( const void* a, const void* b )  {      char *p = (char *)a;      char *q = (char *)b;      if ( abs(*p-*q) == 'a'-'A' )          return *p - *q;      return low(*p) - low(*q);  }    void dfs( int l, int d )  {      /* 去重,从前向后查询,遇到处理位置和上次一样即重复。 */      int flag = 1;      for ( int i = 0 ; i < d ; ++ i )          if ( last[i] != save[i] ) {              flag = 0;break;          }      if ( d && flag ) return;            if ( l == d ) {          strcpy(last,save);          printf("%s\n",save);          return;      }      for ( int i = 0 ; i < l ; ++ i )          if ( !used[i] ) {              used[i] = 1;              save[d] = data[i];              dfs( l, d+1 );              used[i] = 0;          }  }    int main()  {      int t;      while ( scanf("%d",&t) != EOF )      while ( t -- ) {          memset( used, 0, sizeof(used) );          memset( last, 0, sizeof(last) );          memset( save, 0, sizeof(save) );          scanf("%s",data);          int l = strlen(data);          qsort( data, l, sizeof(char), cmp );          dfs( l, 0 );      }            return 0;  }

【额外补充

    是字典序,不是ASC序,AaBb...

转载于:https://my.oschina.net/CoderBleak/blog/665499

你可能感兴趣的文章
jdbcTemplate 调用存储过程。 入参 array 返回 cursor
查看>>
C++中的stack类、QT中的QStack类
查看>>
Linux常用基本命令[cp]
查看>>
CSS 相对|绝对(relative/absolute)定位系列(一)
查看>>
关于 Nginx 配置 WebSocket 400 问题
查看>>
Glide和Govendor安装和使用
查看>>
Java全角、半角字符的关系以及转换
查看>>
Dubbo和Zookeeper
查看>>
前端项目课程3 jquery1.8.3到1.11.1有了哪些新改变
查看>>
UOJ#179. 线性规划(线性规划)
查看>>
整合spring cloud云架构 - SSO单点登录之OAuth2.0登录认证(1)
查看>>
windows的服务中的登录身份本地系统账户、本地服务账户和网络服务账户修改
查看>>
JAVA中循环删除list中元素的方法总结
查看>>
redis 安装
查看>>
SQL some any all
查看>>
电子书下载:Programming Windows Identity Foundation
查看>>
有理想的程序员必须知道的15件事
查看>>
用于测试的字符串
查看>>
财付通和支付宝资料收集
查看>>
PHPCMS V9数据库表结构分析
查看>>