字符串匹配(后缀数组的使用)

题目描述:妞妞有两个字符串a和b,其中a串是一个01串,b串中除了可能有0和1,还可能有'?',b中的'?'可以确定为0或者1。寻找一个字符串t是否在字符串s中出现的过程,称为字符串匹配。牛牛现在考虑所有可能的字符串b,有多少种可以在字符串a中完成匹配。

例如:a="00010001",b="??",字符串b可能的字符串是"00","01","10","11",只有"11"没有出现在字符串a中,所以输出3。

输入描述:输入包括两行,第一行是字符串a,长度在1-50,第二行是字符串b,长度是1-50,输出为一个数,表示匹配数量。

  1 #include "stdio.h"
  2 #include "stdlib.h"
  3 #define MAXLEN 55
  4 int cmp(char * a, char * b){
  5     int i = 0;
  6     while (a[i]!='\0'&&b[i]!='\0') {
  7         if (a[i]<b[i]) {
  8             return 1;
  9         } else if(a[i] > b[i]){
 10             return 2;
 11         } else {
 12             ++i;
 13         }
 14     }
 15     if (a[i]=='\0'&&b[i]=='\0') {
 16         return 0;
 17     } else if(a[i]=='\0'){
 18         return 1;
 19     } else {
 20         return 2;
 21     }
 22 }
 23 int cmp2(char * a, char * b, int len){
 24     int i = 0;
 25     while (a[i]!='\0'&&b[i]!='\0'&& i<len) {
 26         if (a[i]<b[i]) {
 27             return 1;
 28         } else if(a[i] > b[i]){
 29             return 2;
 30         } else {
 31             ++i;
 32         }
 33     }
 34     if (i==len) {
 35         return 0;
 36     }
 37     if (a[i]=='\0'&&b[i]=='\0') {
 38         return 0;
 39     } else if(a[i]=='\0'){
 40         return 1;
 41     } else {
 42         return 2;
 43     }
 44 }
 45 void sort(char** arr, int start, int end){
 46     if (end-start <= 0) {
 47         return;
 48     }
 49     char* pivot = *(char**)((void**)arr + sizeof(char)*start);
 50     int i = start+1;
 51     int j = end;
 52     while (i<=j) {
 53         while (cmp(arr[i],pivot)!=2) {
 54             ++i;
 55         }
 56         while(cmp(arr[j],pivot)==2){
 57             --j;
 58         }
 59         if (i>=j) {
 60             break;
 61         }
 62         char * temp = *(char**)((void**)arr + sizeof(char)*i);
 63         *(char**)((void**)arr + sizeof(char)*i) = *(char**)((void**)arr + sizeof(char)*j);
 64         *(char**)((void**)arr + sizeof(char)*j) = temp;
 65     }
 66 
 67     *(char**)((void**)arr + sizeof(char)*start) = *(char**)((void**)arr + sizeof(char)*j);
 68     *(char**)((void**)arr + sizeof(char)*j) = pivot;
 69     sort(arr, start, j-1);
 70     sort(arr, j+1, end);
 71 }
 72 
 73 int match(char * str, char * pattern, int len){
 74     int i = 0;
 75     while (i<len&&str[i]!='\0'&&pattern!='\0') {
 76         if (pattern[i]=='?') {
 77             ++i;
 78             continue;
 79         }
 80         if (str[i]!=pattern[i]) {
 81             return 1;
 82         }
 83     }
 84     return 0;
 85 }
 86 
 87 int main(int argc, const char* argv[]){
 88     char* str = malloc(sizeof(char)*MAXLEN);
 89     char* pattern = malloc(sizeof(char)*MAXLEN);
 90     scanf("%s",str);
 91     scanf("%s",pattern);
 92     int strlen = 0;
 93     while (str[strlen]!='\0') {
 94         ++strlen;
 95     }
 96     int patternlen = 0;
 97     while (pattern[patternlen]!='\0') {
 98         ++patternlen;
 99     }
100     char **p1 = malloc(sizeof(char*)*MAXLEN);
101     char **p2 = malloc(sizeof(char*)*MAXLEN);
102     for (int i = 0; i <= strlen - patternlen; ++i) {
103         *(char**)((void**)p1 + sizeof(char)*i) = (char* )((void*)str + sizeof(char)*i);
104     }
105     sort(p1, 0,strlen-patternlen);
106     int i = 0;
107     int j = 1;
108     p2[0] = p1[0];
109     while (j <= strlen - patternlen) {
110         if (cmp2(*(char**)((void**)p2 + sizeof(char)*i),*(char**)((void**)p1 + sizeof(char)*j), patternlen) == 0) {
111             ++j;
112             continue;
113         }
114         //p2[++i] = p1[j++];
115         ++i;
116         *(char**)((void**)p2 + sizeof(char)*i) = *(char**)((void**)p1 + sizeof(char)*j);
117         ++j;
118     }
119     strlen = i+1;
120     j = 0;
121     int ans = 0;
122     while(j<strlen){
123         if (match(*(char**)((void**)p2 + sizeof(char)*j), pattern, patternlen) == 0) {
124             ++ans;
125         }
126         ++j;
127     }
128     printf("%d\n",ans);
129     return 0;
130 }

猜你喜欢

转载自www.cnblogs.com/dmzxxmeng/p/12340387.html