2008--2009年北京航空航天大学计算机研究生机试真题
生活随笔
收集整理的這篇文章主要介紹了
2008--2009年北京航空航天大学计算机研究生机试真题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://ac.jobdu.com/problem.php?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; }http://ac.jobdu.com/problem.php?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; }
?
總結
以上是生活随笔為你收集整理的2008--2009年北京航空航天大学计算机研究生机试真题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++简单笔试题
- 下一篇: memset()的效率以及源码分析