Poj-1035-Spell checker

Source: Internet
Author: User

Check whether spelling is correct. Give you a dictionary, and then give you the words you want to query to determine whether a word is correct. The following rules apply: 1. the dictionary contains the word 2, which deletes a letter, 3 adds a letter 4, and replaces any letter.

 

Algorithm: in fact, there is no algorithm, that is, scanning the dictionary and comparing the length of the target string and the dictionary string, in three cases: equal length, one less, one more letter.

Code:

# Include <stdio. h>
# Include <string. h>

Char STR [2, 10010] [20], s [20];
Int used [10010], N;

Int judge ()
{
Int I, J, K, L, M;
Int Len;
Len = strlen (s );
For (I = 0; I <n; I ++)
{
L = strlen (STR [I]);
If (LEN = L) // same length
{
If (strcmp (STR [I], S) = 0)
Return 1;
Else
{
K = 0;
For (j = 0; j <Len; j ++)
{
If (STR [I] [J]! = S [J])
K ++;
}
If (k = 1)
{
Used [I] = 1;
Continue;
}
}

}
Else
{
If (LEN = L-1) // less than 1
{
K = 0; M = 0;
For (j = 0; j <Len; j ++)
{
While (k <L)
{
If (s [J] = STR [I] [k])
{
M ++;
K ++; // do not forget to move back when they are equal. Determines the longest common substring.
Break;
}
Else
K ++;
}
}
If (M = Len)
{
Used [I] = 1;
Continue;
}
}
Else if (LEN = L + 1)
{
K = 0; M = 0;
For (j = 0; j <L; j ++)
{
While (k <Len)
{
If (s [k] = STR [I] [J])
{
M ++;
K ++;
Break;
}
Else
K ++;
}
}
If (M = L)
{
Used [I] = 1;
Continue;
}

}
}
}
Return 0;
}

Int main ()
{
Int I, J;
N =-1;
While (1)
{
N ++;
Scanf ("% s", STR [N]);
If (STR [N] [0] = '#')
Break;
}
While (1)
{
J = 0;
Memset (used, 0, sizeof (used ));
Scanf ("% s", S );
If (s [0] = '#')
Break;
If (Judge ())
Printf ("% s is correct/N", S );
Else
{
Printf ("% s:", S );
For (I = 0; I <n; I ++)
{
If (used [I] = 1)
{
J = 1;
Printf ("% s", STR [I]);
}
}
If (j = 0)
Printf (""); // note that if it is not correct, spaces are still required.
Printf ("/N ");
}
}

Return 0;
}

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.