Poj 1035 spell checker (string processing)

Source: Internet
Author: User
Spell checker
Time limit:2000 ms   Memory limit:65536 K
Total submissions:16675   Accepted:6087

Description

You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given words using a known Dictionary of all correct words in all their forms.
If the word is absent in the dictionary then it can be replaced by correct words (from the dictionary) that can be obtained by one of the following operations:
? Deleting of one letter from the word;
? Replacing of one letter in the word with an arbitrary letter;
? Inserting of one arbitrary letter into the word.
Your task is to write the program that will find all possible replacements from the dictionary for every given word.

Input

The first part of the input file contains all words from the dictionary. each word occupies its own line. this part is finished by the single character '#' on a separate line. all words are different. there will be at most 10000.
Words in the dictionary.
The next part of the file contains all words that are to be checked. each word occupies its own line. this part is also finished by the single character '#' on a separate line. there will be at most 50 words that are to be checked.
All words in the input file (words from the dictionary and words to be checked) consist only of small alphabetic characters and each one contains 15 characters at most.

Output

Write to the output file exactly one line for every checked word in the order of their appearance in the second part of the input file. if the word is correct (I. e. it exists in the dictionary) write the message: "is correct ".
If the word is not correct then write this word first, then write the character ':' (colon), and after a single space write all its possible replacements, separated by spaces. the replacements shocould be written in the order of their appearance in the dictionary
(In the first part of the input file). If there are no replacements for this word then the line feed shoshould immediately follow the colon.

Sample Input

Iishashavebemymorecontestmetooifaward # meawaremcontesthavooorifimre #

Sample output

 
Me is correctaware: awardm: I my mecontest is correcthav: Has haveoo: tooor: I is correctfi: Imre: More me

Source

Northeastern Europe 1998. Question: Give a dictionary and some words. Determine whether these words are correctly spelled. If this word is found in the dictionary, it is correct. The output "% s (this word) is correct ". If it is not found, check whether the dictionary exists. Similar to the word, output these words. It has three meanings: 1. You can add any letter to the word to determine whether the new word exists in the dictionary. 2. You can remove any letter and determine the word. Whether the new word exists. 3. You can change any one of the letters to find whether the new word exists in the dictionary. When it comes to close words, it is recorded. This is the correct way to write words in the input. By question Output. (It is a discriminant system) Analysis: Simulation + brute force! Add a letter to determine whether the word exists, provided that the word is less than the correct word in the dictionary and the remaining letters are the same. Similarly, to reduce a letter, the premise is that the word is better than the word in the dictionary. One more letter and the rest of the letters are the same. Changing a word requires that the words to be judged have the same length and only one letter. Use strcmp () to retrieve input words in the dictionary (). Feelings: note the writing of input. I am not familiar with using new dynamic memory allocation .... Thanks ~!!! Remember to delete and release space .. It is said that this question can also be written in a dictionary tree. Unfortunately, I want to learn "dictionary Tree" first "~! Code:

# Include <cstdio> # include <iostream> # include <cstring> using namespace STD; char dict [10005] [20]; char word [55] [20]; int dictnum, wordnum; void input () {While (scanf ("% s", & dict [dictnum]) & dict [dictnum ++] [0]! = '#'); While (scanf ("% s", & word [wordnum]) & word [wordnum ++] [0]! = '#'); Dictnum --; // remove "#" wordnum --;} bool change (char * dict, char * Word) {int DIF = 0; while (* Word) {If (* (word ++ )! = * (Dict ++) {DIF ++; If (DIF> 1) return false ;}} return true ;} bool add (char * dict, char * word) {int DIF = 0; while (* dict) {If (* dict! = * Word) {dict ++; DIF ++; If (DIF> 1) return false;} else {dict ++; Word ++ ;}} return true ;} bool del (char * dict, char * Word) {int DIF = 0; while (* Word) {If (* word! = * Dict) {word ++; DIF ++; If (DIF> 1) return false;} else {word ++; dict ++ ;}} return true ;} int main () {input (); int * dictlen = new int [dictnum]; for (INT I = 0; I <dictnum; I ++) dictlen [I] = strlen (dict [I]); For (Int J = 0; j <wordnum; j ++) {int Len = strlen (word [J]); int * address = new int [dictnum]; int Pa = 0; int flag = false; For (int K = 0; k <dictnum; k ++) {If (LEN = dictlen [k]) {If (strcmp (dict [K], word [J]) = 0) {flag = true; break ;} else if (Change (dict [K], word [J]) Address [PA ++] = K;} else if (LEN-dictlen [k] = 1) {If (DEL (dict [K], word [J]) Address [PA ++] = K;} else if (dictlen [k]-len = 1) {If (add (dict [K], word [J]) Address [PA ++] = K ;}} if (FLAG) printf ("% s is correct \ n", word [J]); else {printf ("% s:", word [J]); For (int d = 0; d <Pa; D ++) printf ("% s", dict [Address [d]); printf ("\ n") ;}delete address ;} return 0 ;}

11933189

Fukan

1035

Accepted

424 K

94 Ms

C ++

2480b

2013-08-06 20:36:48

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.