This problem originally card I do not know how many times, every time I want to do a CSP certification of the third problem, all start from this problem, but each time is stuck. Directly even test instructions are a little read, but after reading it will find that the problem in addition to the special cumbersome, it seems useless to what difficult algorithm.
Draw on the idea of this big guy: 79962425?utm_source=blogxgwz0
I did not find a more concise code than this, so I learned a bit of this idea, it is relatively easy to understand.
I really don't know what to do with this problem. The reason is that: 1. String matching problem did too little, did not do a few. 2. The StringStream flow operation is not very good, so it is not flexible enough for complex string operations. 3. The data structure is not thought out, do not know what to save so many strings.
The main idea is this, give a series of matching rules, and a series of strings to be matched. For each string to be matched, to match it with each rule, if the match succeeds, the name of the rule is output, and the parameter portion of the successful match (that is, the part of str, int, path successfully matched in the title, the other successful match does not output), and if all rules do not match, the output is 404.
Open a one-dimensional array of strings, hold all the original rules, and then open a two-dimensional character array, storing each original rule is '/' split after the part. Open another one-dimensional array of integers, storing the number of pieces after each primitive rule is split, and then opening an array of tokens to mark whether each original array is '/' ending.
After a double loop, each match is compared to each rule, and if one is set up, it jumps out of the way. The matching function is a module. The matching of each statement is to be split, part of the match, which requires a split function;
Here is the code, some key places have comments. In fact, it is difficult to split the string and matching on the, data structure can not think out, split will not, the problem is 0 points.
There are a lot of small details in the program, the smart place, need to carefully understand.
#include <bits/stdc++.h> #define N 105using namespace std;string lim[n],lims[n][n],limname[n]; Lim is the i-rule string, the LIMs is a split of the I-rule string, and Limname is the name of the corresponding string, string now,nows[n]; Nows is the split bool Isop[n],iso of the currently retrieved string; Isop There is no slash at the end of the judgment rule, and the ISO determines whether there is a slash int limcnt[n],nowcnt at the end of the address; LIMCNT the number of split latter for the count rule, nowcnt counts the split latter number of the currently retrieved string, void Stcut (string ori,string cut[],bool& is,int&num); bool Match ( int J,int nc,string& a); string Isnum (string s); int main () {int n,m; cin>>n>>m; for (int i=0;i<n;i++) {cin>>lim[i]>>limname[i]; Stcut (Lim[i],lims[i],isop[i],limcnt[i]); } for (int i=0;i<m;i++) {cin>>now;iso=0; Note that ISO 0 stcut (now,nows,iso,nowcnt) is placed here; String ans; int f=0; for (int j=0;j<n;j++) {if (Match (J,nowcnt,ans)) {string Ss=limname[j]+ans; cout<<ss<<endl; F=1;break; }} if (f==0) cout<<404<<endl; } return 0;} Nows is the current string, J is the current determinant module ordinal, NC is the number of items after the current string segmentation, and a is possible with the answer string following the label bool match (int j,int nc,string& a) {a= ""; int p1=0,p2=0; while (P1<nc&&p2<limcnt[j]) {if (nows[p1]==lims[j][p2]); else if (lims[j][p2]== "<int>") {string st=isnum (Nows[p1]); if (st== ") return 0; else a+= ' +st; } else if (lims[j][p2]== "<path>") {a+= ' +nows[p1++]; while (P1<NC) a+= '/' +nows[p1++]; if (ISO) a+= '/'; return 1; } else if (lims[j][p2]== "<str>") a+= ' +nows[p1]; else return 0; p1++,p2++; } if (p1<nc| | P2<LIMCNT[J]) return 0; if (Isop[j]^iso) return 0; return 1;} The Ori is the original string, cut is the cut-out block, is the last to be judged to have no '/', NUM is the number of items after the Shard void Stcut (string ori,string cut[],bool& is,int& num) {int len= Ori.size (); string SS; Num=0; if (ori[len-1]== '/') Is=1; for (int i=0;i< (int) ori.size (); i++) if (ori[i]== '/') ori[i]= '; StringStream in (ORI); while (IN>>SS) cut[num++]=ss;;} String Isnum (String s) {string ans= ""; int ok=0; for (int i=0;i< (int) s.size (); i++) {if (s[i]< ' 0 ' | | S[i]> ' 9 ') return ""; else if (ok| | (s[i]> ' 0 ' &&s[i]<= ' 9 ')) ans+=s[i],ok=1; } return ans;
201803-4 Chess Evaluation