【庞果网英雄会】4月第4次面试集训#字符串#:字符串匹配问题
2013-05-15 21:04
441 查看
题目详情
字符串匹配问题,给定一串字符串,按照指定规则对其进行匹配,并将匹配的结果保存至output数组中,多个匹配项用空格间隔,最后一个不需要空格。要求:
匹配规则中包含通配符?和*,其中?表示匹配任意一个字符,*表示匹配任意多个(>=0)字符。
匹配规则要求匹配最大的字符子串,例如a*d,匹配abbdd而非abbd,即最大匹配子串。
匹配后的输入串不再进行匹配,从当前匹配后的字符串重新匹配其他字符串。
要求实现函数:char* my_find(char input[], char rule[])
举例说明:
input:abcadefg
rule:a?c
output:abc
input :newsadfanewfdadsf
rule: new
output: new new
input :breakfastfood
rule: f*d
output:fastfood
代码:
// Regexpr.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string.h> #include <assert.h> char *g_pStack[200]; int g_nDeep = 0; char* my_find(char input[], char rule[]); int Match(char *p, char *&pattern, char **pEnd); int SearchAllChar(char *p, char ch); int main(int argc, char* argv[]) { char *output = NULL; if (output = my_find("akfasdofoodx", "f*do")) printf("%s\r\n", output); assert(0 == strcmp(output, "fasdo")); delete output; if (output = my_find("akfasdofoodx", "a*fa*d")) printf("%s\r\n", output); assert(0 == strcmp(output, "akfasdofood")); delete output; if (output = my_find("akfasdofoodx", "a*fa*do")) printf("%s\r\n", output); assert(0 == strcmp(output, "akfasdo")); delete output; if (output = my_find("akfasdofoodx", "a**f**d")) printf("%s\r\n", output); assert(0 == strcmp(output, "akfasdofood")); delete output; if (output = my_find("akfasdofoodx", "a**f**x")) printf("%s\r\n", output); assert(0 == strcmp(output, "akfasdofoodx")); delete output; if (output = my_find("akfasdofoodx", "**f**x")) printf("%s\r\n", output); assert(0 == strcmp(output, "akfasdofoodx")); delete output; if (output = my_find("akfasdofoodx", "**f**")) printf("%s\r\n", output); assert(0 == strcmp(output, "akfasdofoodx")); delete output; if (output = my_find("akfasdofoodx", "f*dx")) printf("%s\r\n", output); assert(0 == strcmp(output, "fasdofoodx")); delete output; if (output = my_find("abcadcfg", "a?c")) printf("%s\r\n", output); assert(0 == strcmp(output, "abc adc")); delete output; if (output = my_find("abcadcfg", "a?*c")) printf("%s\r\n", output); assert(0 == strcmp(output, "abcadc")); delete output; if (output = my_find("newsadfanewfdadsf", "new")) printf("%s\r\n", output); assert(0 == strcmp(output, "new new")); delete output; if (output = my_find("newsadfaneswfdadsf", "new*d*s")) printf("%s\r\n", output); assert(0 == strcmp(output, "newsadfaneswfdads")); delete output; return 0; } void mystrncpy(char *pDest, char *pScr, int n) { int i; for (i = 0; i < n; i++) { pDest[i] = pScr[i]; } pDest[i] = ' '; } char* my_find(char input[], char rule[]) { printf("Text is:\"%s\", find pattern:\"%s\"\r\n", input, rule); printf(" result "); int nFind = 0; char *pattern = rule; int nIdx = 0; int nLen = 0; char *pStart = input; while('\0' != *pStart++) nLen++; pStart = input; char *output = new char[nLen+1]; while ('\0' != *pStart) { char *pEnd = NULL; int bMatch = Match(pStart, pattern, &pEnd); if (bMatch) { mystrncpy(&output[nIdx], pStart, pEnd-pStart); nIdx += pEnd-pStart+1; nFind++; pStart = pEnd; if ('*' == *(pattern-1)) { nIdx--; while (*pStart) { output[nIdx++] = *pStart++; } output[nIdx++] = '\0'; break; } } else { pStart++; } pattern = rule; } if (0 != nFind) { output[nIdx-1] = 0; } return output; } int Match(char *p, char *&pattern, char **pEnd) { int bPreWildchar = 0; int nFind = NULL; while ('*' == *pattern) { bPreWildchar = 1; pattern++; } *pEnd = p; if ('\0' == *pattern) { return 1; } if ('\0' == *p) { return 0; } if (bPreWildchar) { nFind = SearchAllChar(p, *pattern); while (0 != nFind--) { p = g_pStack[--g_nDeep]; char * tmp = pattern; if (Match(++p, ++pattern, pEnd)) return 1; pattern = tmp; } return 0; } if ('?' == *pattern || *pattern == *p) { return Match(p+1, ++pattern, pEnd); } return 0; } int SearchAllChar(char *p, char ch) { int nFind = 0; while (*p) { if (ch == *p) { g_pStack[g_nDeep++] = p; nFind++; } p++; } return nFind; }
运行结果:
Text is:"akfasdofoodx", find pattern:"f*do" result fasdo Text is:"akfasdofoodx", find pattern:"a*fa*d" result akfasdofood Text is:"akfasdofoodx", find pattern:"a*fa*do" result akfasdo Text is:"akfasdofoodx", find pattern:"a**f**d" result akfasdofood Text is:"akfasdofoodx", find pattern:"a**f**x" result akfasdofoodx Text is:"akfasdofoodx", find pattern:"**f**x" result akfasdofoodx Text is:"akfasdofoodx", find pattern:"**f**" result akfasdofoodx Text is:"akfasdofoodx", find pattern:"f*dx" result fasdofoodx Text is:"abcadcfg", find pattern:"a?c" result abc adc Text is:"abcadcfg", find pattern:"a?*c" result abcadc Text is:"newsadfanewfdadsf", find pattern:"new" result new new Text is:"newsadfaneswfdadsf", find pattern:"new*d*s" result newsadfaneswfdads
相关文章推荐
- java实现字符串匹配问题之求两个字符串的最大公共子串
- 工作随笔——壹(关于MYSQL数据库中字符串与字符串匹配问题)
- 字符串转换成整数,通配符的字符串匹配问题
- 一个笔试面试经常问的问题——统计字符串中出现最多的字符及次数
- 算法实现字符串匹配问题:从三个字符串中找出共有的最长部分。
- 字符串转换成整数,字符串匹配问题
- java实现字符串匹配问题之求两个字符串的最大公共子串
- 子字符串substring 问题 - KMP 字符串匹配算法备忘录
- 一个C#面试问题,要求是将字符串中重复字符从第二个开始都去掉,空格除外。然后显示的时候,从后往前显示。
- HDU 3746 字符串匹配(字符串的最小循环节问题)
- 字符串转换成整数&带通配符的字符串匹配问题
- 字符串匹配问题——求给定字符串的next数组以及KMP算法实现
- 字符串匹配问题(int countABC(* s) 输入任何一串字符串,计算机其
- 面试经典--字符串匹配问题
- C 字符数组与字符串的使用及加结束符'\0'的问题
- Java 字符串匹配和替换字符串
- 中文字符串匹配问题
- 问题 F: 字符串匹配问题
- 带通配符的字符串匹配问题
- 在字符串中筛选出数字字符而发现的若干问题