您的位置:首页 > 其它

POJ 3267

2011-01-31 11:15 92 查看
DP问题。

思路:

每个字符有两种情况:作为单词首字母和不作为单词首字母。

gaiRemovedNum[i]表示从i开始到结尾需要删除的字符数;

gaiRemovedNum[i] = MIN(将第i的字符作为首字母匹配时需要删除的字符数 , 1 + gaiRemovedNum[i+1]);

(1 + gaiRemovedNum[i+1])是i不作为首字母时需要删除的字符数。

从最后一个字符开始扫描,则gaiRemovedNum[0]即为所求。



/*The Cow Lexicon
Time Limit: 2000MS  Memory Limit: 65536K
Total Submissions: 4374  Accepted: 1959

Description

Few know that the cows have their own dictionary with W (1 ≤ W ≤ 600) words, each containing no more 25 of the characters 'a'..'z'. Their cowmunication system, based on mooing, is not very accurate; sometimes they hear words that do not make any sense. For instance, Bessie once received a message that said "browndcodw". As it turns out, the intended message was "browncow" and the two letter "d"s were noise from other parts of the barnyard.

The cows want you to help them decipher a received message (also containing only characters in the range 'a'..'z') of length L (2 ≤ L ≤ 300) characters that is a bit garbled. In particular, they know that the message has some extra letters, and they want you to determine the smallest number of letters that must be removed to make the message a sequence of words from the dictionary.

Input

Line 1: Two space-separated integers, respectively: W and L
Line 2: L characters (followed by a newline, of course): the received message
Lines 3..W+2: The cows' dictionary, one word per line
Output

Line 1: a single integer that is the smallest number of characters that need to be removed to make the message a sequence of dictionary words.
Sample Input

6 10
browndcodw
cow
milk
white
black
brown
farmer
Sample Output

2
*/

#include <stdio.h>
#include "stdlib.h"
#include "string.h"
#define MAX_DICTIONARY_WORD_NUM 605
#define MAX_WORD_LEN 30
#define MAX_MESSAGE_LEN 305

typedef struct _WORD_ST_
{
int iLen;
char acWord[MAX_WORD_LEN];
}WORD_ST;

typedef struct _DICTIONARY_ST_
{
int iWordNum;
WORD_ST astWord[MAX_DICTIONARY_WORD_NUM];
}DICTIONARY_ST;

typedef struct _DICTIONARY_INDEX_ST_
{
int iBeginIndex;
int iEndIndex;
}DICTIONARY_INDEX_ST;

#define INDEX(x) ((x) - 'a')
#define MIN(a,b)  (((a) < (b)) ? (a) : (b))

DICTIONARY_ST gstDict;
DICTIONARY_INDEX_ST gastDictIndex[26];
int gaiRemovedNum[MAX_MESSAGE_LEN] = {0};

int WordCmp(const void *a,const void *b)
{

return ((WORD_ST *)a)->acWord[0] - ((WORD_ST *)b)->acWord[0];
}

int TheCowLexiconmain(void)
{
char acMessage[MAX_MESSAGE_LEN];
char cTempChar;
char c1stCharOfWord;
int  iMessageLen;
int  iLoopMessage;
int  iLoopMessageChar;
int  iLoopWord;
int  iLoopWordChar;
int  iRemovedNum;
int  iMinRemovedNum = MAX_MESSAGE_LEN;

memset(gaiRemovedNum,0,MAX_MESSAGE_LEN*sizeof(int));

scanf("%d %d",&gstDict.iWordNum,&iMessageLen);
scanf("%s",acMessage);

for(iLoopWord = 0; iLoopWord < gstDict.iWordNum; iLoopWord++)
{
scanf("%s",&gstDict.astWord[iLoopWord].acWord);
gstDict.astWord[iLoopWord].iLen = strlen(gstDict.astWord[iLoopWord].acWord);
}
qsort(gstDict.astWord,gstDict.iWordNum,sizeof(WORD_ST),WordCmp);

/************************************************************************/
/* creat index                                                          */
/************************************************************************/
cTempChar = gstDict.astWord[0].acWord[0];
gastDictIndex[INDEX(cTempChar)].iBeginIndex = 0;
for(iLoopWord = 1; iLoopWord < gstDict.iWordNum; iLoopWord++)
{
if (cTempChar != gstDict.astWord[iLoopWord].acWord[0])
{
gastDictIndex[INDEX(cTempChar)].iEndIndex = iLoopWord-1;
cTempChar = gstDict.astWord[iLoopWord].acWord[0];
gastDictIndex[INDEX(cTempChar)].iBeginIndex = iLoopWord;
}
}
gastDictIndex[INDEX(cTempChar)].iEndIndex = gstDict.iWordNum-1;

/************************************************************************/
/* resolve                                                              */
/************************************************************************/

for (iLoopMessage = iMessageLen-1; iLoopMessage >= 0; iLoopMessage--)
{
c1stCharOfWord = acMessage[iLoopMessage];
if (iLoopMessage == iMessageLen-1)
{
iMinRemovedNum = 1;
}
else
{
iMinRemovedNum = 1+gaiRemovedNum[iLoopMessage+1];
}

for (iLoopWord = gastDictIndex[INDEX(c1stCharOfWord)].iBeginIndex; iLoopWord <= gastDictIndex[INDEX(c1stCharOfWord)].iEndIndex; iLoopWord++)
{
iRemovedNum = 0;
iLoopWordChar = 0;
iLoopMessageChar = iLoopMessage;
while ( iLoopWordChar < gstDict.astWord[iLoopWord].iLen&&iLoopMessageChar < iMessageLen )
{
if (gstDict.astWord[iLoopWord].acWord[iLoopWordChar] == acMessage[iLoopMessageChar])
{
iLoopWordChar++;
iLoopMessageChar++;
}
else
{
iLoopMessageChar++;
iRemovedNum++;
if (iRemovedNum >= iMinRemovedNum)
{
break;
}
}
}
if(iLoopWordChar == gstDict.astWord[iLoopWord].iLen)
{
iRemovedNum += gaiRemovedNum[iLoopMessageChar];
}
else
{
iRemovedNum = iMessageLen - iLoopMessage;
}
iMinRemovedNum = MIN(iMinRemovedNum,iRemovedNum);
}

gaiRemovedNum[iLoopMessage] = iMinRemovedNum;
}

printf("%d/n",gaiRemovedNum[0]);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: