POJ1318解题报告
来源:互联网 发布:2015淘宝新店扶持多久 编辑:程序博客网 时间:2024/06/05 09:40
Word Amalgamation
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 6105 Accepted: 3041
Description
In millions of newspapers across the United States there is a word game called Jumble. The object of this game is to solve a riddle, but in order to find the letters that appear in the answer it is necessary to unscramble four words. Your task is to write a program that can unscramble words.
Input
The input contains four parts: 1) a dictionary, which consists of at least one and at most 100 words, one per line; 2) a line containing XXXXXX, which signals the end of the dictionary; 3) one or more scrambled 'words' that you must unscramble, each on a line by itself; and 4) another line containing XXXXXX, which signals the end of the file. All words, including both dictionary words and scrambled words, consist only of lowercase English letters and will be at least one and at most six characters long. (Note that the sentinel XXXXXX contains uppercase X's.) The dictionary is not necessarily in sorted order, but each word in the dictionary is unique.
Output
For each scrambled word in the input, output an alphabetical list of all dictionary words that can be formed by rearranging the letters in the scrambled word. Each word in this list must appear on a line by itself. If the list is empty (because no dictionary words can be formed), output the line "NOT A VALID WORD" instead. In either case, output a line containing six asterisks to signal the end of the list.
Sample Input
tarpgivenscorerefundonlytrapworkearncoursepepperpartXXXXXXresconfudreaptrsettoresucXXXXXX
Sample Output
score******refund******parttarptrap******NOT A VALID WORD******course******
核心思想:查找在字典中含有的字母。首先将字典进行按字母升序进行排序,将词表也按照字母的升序进行排序。然后看字母在词典中是否存在。对于存在的词,进行排序后再输出。
代码如下:(力求能够写好C程序,呵呵,对JAVA比较熟,但用JAVA处理字符串算是取巧了...)
#include<stdio.h>#include<string.h>#define MAXNUM 100#define MAXLEN 6//存储词的字典 char *dictinary[MAXNUM];char *words[MAXNUM];//存放排序后的字典 char* trans[MAXNUM];char* record[MAXNUM]; int partion(char *str,int i,int j){ int m=i,n=j+1; char ch=str[i]; char temp; while(1){ while(str[++m]<ch&&m<j); while(str[--n]>ch); if(m>=n) { break; } temp=str[m]; str[m]=str[n]; str[n]=temp; } str[i]=str[n]; str[n]=ch; return n;}void QuickSort(char *str,int i,int j){ if(i<j) { int p=partion(str,i,j); QuickSort(str,i,p-1); QuickSort(str,p+1,j); }}int main(){ //len1和len2与真实的长度都相差1 int len1=-1; int len2=-1; int index=-1; int i; int j; int k; int tag; char *te; te=(char*)malloc(sizeof(char)*MAXLEN); for(i=0;i<MAXNUM;i++) { record[i]=(char*)malloc(sizeof(char)*MAXLEN); } while(1) { dictinary[++len1]=(char*)malloc(sizeof(char)*MAXLEN); trans[len1]=(char*)malloc(sizeof(char)*MAXLEN); scanf("%s",dictinary[len1]); //做一个备份 strcpy(trans[len1],dictinary[len1]); if(strcmp(dictinary[len1],"XXXXXX")==0) { len1--; break; } } while(1) { words[++len2]=(char*)malloc(sizeof(char)*MAXLEN); scanf("%s",words[len2]); if(strcmp(words[len2],"XXXXXX")==0) { len2--; break; } } //对字典和词表按字母的升序进行排序 for(i=0;i<=len1;i++) { QuickSort(trans[i],0,strlen(dictinary[i])-1); } for(i=0;i<=len2;i++) { QuickSort(words[i],0,strlen(words[i])-1); } //输出 for(i=0;i<=len2;i++) { tag=1; index=-1; for(j=0;j<=len1;j++) { if(strcmp(words[i],trans[j])==0) { tag=0; record[++index]=dictinary[j]; } } if(tag) { printf("NOT A VALID WORD\n"); } else { //冒泡排序 for(j=0;j<=index-1;j++) { for(k=0;k<=index-1-j;k++) { if(strcmp(record[k],record[k+1])>0) { strcpy(te,record[k]); strcpy(record[k],record[k+1]); strcpy(record[k+1],te); } } } for(j=0;j<=index;j++) { printf("%s\n",record[j]); } } printf("******\n"); } free(te); free(dictinary); free(words); free(trans); system("pause"); return 0; }
- POJ1318解题报告
- Word Amalgamation(poj1318) 解题报告
- poj1318
- poj1318
- poj1318
- POJ1318
- poj1318
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- Poj1318 解密
- Antiprime解题报告
- expr解题报告
- 华容道解题报告
- 正则表达式从入门到精通
- 查找到文件后复制到另一目录 目录结构保持不变
- 详细讲解在Spring中进行集成测试
- spring如何使用多个xml配置文件
- Javascript实现动态菜单添加
- POJ1318解题报告
- hibernate里使用JDBC查询示例代码
- tizen alpha 源码下载,sdk下载,sdk初体验
- JavaScript 正则表达式校验日期格式
- 雷锋读图:2011风险投资创10年历史新高,总金额达306亿美金
- linux apt-get yum 软件包
- extern“c”声明的作用
- pthread_create函数使用
- 嵌入式Web服务器:GoAhead和AppWeb