北航研究生复试2008上机第三题:字符串匹配
2018-02-06 15:55
369 查看
题目
从string.in中读入数据,然后用户输入一个短字符串。要求查找string.in中和短字符串的所有匹配,输出行号、匹配字符串到string.out文件中。匹配时不区分大小写,并且可以有一个用中括号表示的模式匹配。如“aa[123]bb”,就是说aaa1bb、aa2bb、aa3bb都算匹配。
主要部分:1、读写文件
2、KMP匹配或者BF匹配算法
3、不区分大小写的比较
4、对于输入的处理(主要是带‘[ ]’的处理)
提示:因为北航上机的要求,编译器为VC6.0。如果用vs2015会存在部分函数不安全的提示如fopen等。
代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> char* file_in = "string.in"; //要读的文件 char* file_out = "string.out"; //要写的文件 int temp = 0; //标记 是否有匹配项 有则改为1 char* ReadFile(char* filename); //读文件到字符数组 char** GetInputStr(int &kind, int &len); //从控制台得到短字符串 void Match_KMP(char* note, int size, char* str, int len); //KMP匹配 同时将匹配字符写入文件file_out void GetNext(char* str,int len,int* next); //求KMP辅助数组NEXT void WriteFile(int line, char* str, int len); //写文件 bool Equal(char a,char b); //比较两个字符是否相等,忽略大小写 int main(){ char* note = NULL; //记录文件中的字符串 note = ReadFile(file_in); int size = strlen(note); printf("the size of file :%d\n",size); printf("the content of file is: \n%s\n",note); int kind = 0; //输入的短字符的种类,如果有[],则分为多组 int len = 0; //短字符每组的的大小 char** str = NULL; //记录短字符串数组 str = GetInputStr(kind,len); printf("the kind of str :%d\n",kind); printf("the size of str :%d\n",len); int i; for(i = 0; i < kind; i++) printf("the content of str is:%s\n",str[i]); for(i = 0; i < kind; i++)//对每一组段字符串进行匹配 Match_KMP(note,size,str[i],len); if(temp == 0){ printf("文件中没有匹配项\n"); } return 0; } char* ReadFile(char* filename){ //estimate the size of file int size = 0; int ch; FILE* file; if((file = fopen(filename,"r")) == NULL){ printf("The file %s can not be opened\n", filename); exit(1); } while((ch = fgetc(file)) != EOF){ //第一次遍历得到文件的大小 ++size; } rewind(file); char* note = (char* )malloc(sizeof(char) * (size+1)); note[size] = '\0'; int i; for(i = 0; i < size; i++){ //第二次遍历取文件的数据 ch = fgetc(file); note[i] = ch; } fclose(file); return note; } char** GetInputStr(int &kind, int &len){ printf("输入短字符串大小: "); scanf("%d",&len); printf("输入一个短字符串:"); char* str = (char* )malloc(sizeof(char) * (len + 1)); scanf("%s", str); str[len] = '\0'; char** arr; //有没有aa[123]bb形式 int tag1 = 0; //记录'['在字符串中的位置 int tag2 = 0; //记录']'在字符串中的位置 int i,j,k; for(i = 0; i < len; i++){ if(str[i] == '[') tag1 = i; if(str[i] == ']') tag2 = i; } if(tag1 && tag2 && tag2 > tag1){ //有多组需要匹配 int num = tag2 - tag1 - 1; //短字符串的个数 arr = (char** ) malloc(sizeof(char* ) * num); for(i = 0; i < num; i++){ arr[i] = (char* )malloc(sizeof(char) * (len - num)); } k = 0; for(i = 0; i < num; i++){ //赋值每个段字符串 for(j = 0; j < len; j++){ if(j < tag1) arr[i][k++] = str[j]; if(j == tag1) arr[i][k++] = str[tag1 + 1 + i]; if(j >= tag2) arr[i][k++] = str[j + 1]; } k = 0; } kind = num; //短字符串的个数 len = len - num - 1; //每个段字符串的大小 } else{ //只有一组短字符串 arr = (char** ) malloc(sizeof(char* )); kind = 1; arr[0] = str; } return arr; } void Match_KMP(char* note, int size, char* str, int len){ if(len <= 0){ printf("子串输入错误\n"); return ; } int i,j; int *next = (int *)malloc(sizeof(int) * len); GetNext(str,len,next); //计算NEXT[]辅助数组 // printf("the Next[]:"); for(i = 0; i < len; i++) printf("%d ",next[i]); printf("\n"); j = 0; int line = 0; //记录行号 for(i = 0; i < size;){ //进行匹配 if(note[i] == '\n') ++line; if(Equal(note[i],str[j])){ //进行字符比较 ++i;++j; if(j == len){ //成功完全匹配 WriteFile(line,¬e[i - j],len); //写入文件 temp = 1; //存在匹配项 } }else{ if(j == 0){ ++i; }else{ j = next[j - 1]; } } } } void GetNext(char* str,int len,int* next){ next[0] = 0; int i,j; i = 1;j = 0; for(; i < len ;){ if(j == 0 || Equal(str[i-1],str[j-1])){ next[i] = ++j; i++; }else{ j = next[j - 1]; } } } void WriteFile(int line, char* str, int len){ FILE* file; int i; if((file = fopen(file_out,"a")) == NULL){ printf("The file %s can not be wirtten\n",file_out); exit(1); } fputc((line + 1 + '0'),file); fputc(' ',file); for(i = 0; i < len; i++) fputc(str[i],file); fputc('\n',file); fclose(file); } bool Equal(char a,char b){ if(b >='0' && b <= '9') if(b == a) return true; if(b >= 'a' && b <= 'z') if(b == a || b - ('a' - 'A') == a) return true; if(b >= 'A' && b <= 'Z') if(b == a || b + ('a' - 'A') == a) return true; return false; }
例子(本人随便弄的):
1、
string.in:
Who has seen the winD?
Neither I nor you;
But when the leaves hang trembling,
The wInd is passing through.
Who has seen the WIND?
Neither you nor I;
But when the trees bow down their heads,
The Wind is passing by.
–Christina Rossett
控制台输入:
4
wind
2、
string.in:
Aa1bb
aA2Bb
aa3BB
aa4Ab
aA5bB
控制台输入:
9
aa[123]bb
结果:
1、
2、
相关文章推荐
- 北航研究生复试2010上机第三题:数组是否相同
- 北航研究生复试2009上机第三题:字符串查找删除
- 北航研究生复试2015上机第三题:记录单词并按字典顺序输出
- 北航研究生复试2013上机第三题:科学计数表示法
- 北航研究生复试2012上机第三题:统计关键字出现的位置
- 北航研究生复试2014上机第三题:冒号对齐
- 北航研究生复试2011上机第三题:显示省略
- 北航上机复试2012-第一题-第二题-第三题
- 2008北航:字符串匹配
- hdu1879浙大计算机研究生复试上机(2008)继续畅通工程
- 继续畅通工程(2008浙江大学研究生复试上机题[最小生成树] hdoj 1879 )
- 字符串匹配--北航机试2008
- 浙大计算机研究生复试上机考试-2009年
- ZJU-PAT 1057. Stack (30) 浙大2013年上机复试第三题
- sort+结构体练习_稳定排序 2008浙大研究生复试热身赛(2)——全真模拟
- 2005年浙大研究生复试上机真题-最大连续子序列
- 2009年西电计算机研究生复试上机题(3)
- hdu 4416 水题 浙大计算机研究生复试上机考试-2005年 可是发现自己写代码有问题
- 浙大计算机研究生复试上机考试-2010年 .
- 清华大学 2011年研究生复试上机题 解题报告