codeforces-1023 A Single Wildcard Pattern Matching

 1 #include <iostream>
 2 #include <unordered_map>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <queue>
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     int n,m;
12     while(cin >> n >> m)
13     {
14         string s,t;
15         cin >> s >> t;
16         if(m+1<n)
17         {
18             cout << "NO" << endl;
19             continue;
20         }
21         int s_index = 0;
22         int t_index = 0;
23         int flag = 0;
24         for(int i = 0; i < n; i ++)
25         {
26             if(s[i]=='*')
27                 break;
28             else if(i==n-1 && m!=n)
29             {
30                 cout << "NO" << endl;
31                 flag = 1;
32                 break;
33             }
34         }
35         if(flag!=1)
36         {
37             while(t_index<m)
38             {
39                 if(s[s_index]=='*')
40                 {
41                     flag = 2;
42                     break;
43                 }
44                 else
45                 {
46                     if(s[s_index]!=t[t_index])
47                     {
48                         cout << "NO" << endl;
49                         flag = 1;
50                         break;
51                     }
52                     s_index ++;
53                     t_index ++;
54                 }
55             }
56         }
57         if(flag==2)
58         {
59             s_index = n-1;
60             t_index = m-1;
61             while(s[s_index]!='*')
62             {
63                 if(s[s_index]!=t[t_index])
64                 {
65                     cout << "NO" << endl;
66                     flag = 1;
67                     break;
68                 }
69                 s_index --;
70                 t_index --;
71             }
72             if(flag==2)
73                 flag = 0;
74         }
75         if(flag==0)
76             cout << "YES" << endl;
77     }
78     return 0;
79 }

猜你喜欢

转载自www.cnblogs.com/Asurudo/p/9496135.html