2009年北京航空航天大学计算机研究生机试真题

?pid=1165字符串匹配

#include<iostream>#include<cstdio>#include<cstring>using namespace std;char str[1005][500],temp[1005][500],pattern[1005];inline bool pre_match(int j,int start,int end){int i;for(i=start;i<=end;i++){if(temp[j][i]!=pattern[i])return false;}return true;}inline bool middle_match(int j,int start,int end){int i;for(i=start+1;i<end;i++){if(temp[j][start]==pattern[i])return true;}return false;}inline bool post_match(int j,int start,int end){int i,p;for(i=start+1,p=end+1;temp[j][i]!=’\0′ && pattern[p]!=’\0′ ;i++,p++){if(temp[j][i]!=pattern[p])return false;}return true;}int main(void){int i,j,n,st,en;while(scanf("%d",&n)!=EOF){for(i=0;i<n;i++){scanf("%s",str[i]);strcpy(temp[i],str[i]);}for(i=0;i<n;i++){for(j=0;temp[i][j]!=’\0′;j++){if(temp[i][j]>=’a’) //将字符串中的字符全部转换为大写,数字则不变temp[i][j]-=32;}}scanf("%s",pattern);st=en=-1;for(i=0;pattern[i]!=’\0′;i++){if(pattern[i]>=’a’)pattern[i]-=32;if(pattern[i]=='[‘)st=i;else if(pattern[i]==’]’)en=i;}if(st==-1) //不存在中括号表示的模式匹配{for(i=0;i<n;i++){if(strcmp(temp[i],pattern)==0){printf("%d %s\n",i+1,str[i]);break;}}}else{for(i=0;i<n;i++){if(strlen(str[i]) != strlen(pattern) – (en – st) )continue;if(pre_match(i,0,st-1) && middle_match(i,st,en) && post_match(i,st,en)) //前缀、中间、后缀分别都匹配{printf("%d %s\n",i+1,str[i]);}}}}return 0;}

?pid=1168字符串的查找删除

#include<iostream>#include<cstdio>using namespace std;#include<string.h>char str[1000][1000],s[1000];int slen;bool match(int j,int len,int index){int i,p;for(i=j,p=0;i<j+len && p<slen;i++,p++){if(s[p]!=str[index][i]){if(s[p]-str[index][i]!=32 && str[index][i]-s[p]!=32) //大小写不敏感return false;}}return true;}int main(void){int i,j,n=0,len;//freopen("a.txt","w",stdout);scanf("%s",s);slen=strlen(s);getchar();while(gets(str[n])){if(strcmp(str[n],"}")==0)break;n++;}for(i=0;i<=n;i++){len=strlen(str[i]);for(j=0;j<=len-slen;j++){if(str[i][j]==’ ‘)continue;if(!match(j,slen,i)) //不匹配的话直接输出printf("%c",str[i][j]);else //匹配的话直接跳过,不输出j=j+slen-1;}for(;j<len;j++)printf("%c",str[i][j]);printf("\n");}return 0;}

,接受失败等于回归真实的自我,

2009年北京航空航天大学计算机研究生机试真题

相关文章:

你感兴趣的文章:

标签云: