Ideas:
html structure and mapping relationships, based on the input html text, look for its structure, if no structure is found to match, output" can "T  Identify " If there is only one, the output is based on the requirements of the mapping relationship and output format, and if there are multiple, the first input structure is used.
1 HTML input read
2, the judging structure is not the input HTML text
3 output results according to the requirements of the mapping and output format
Note that there may be a id to multiple tags, And there are multiple of these tags in the input format.
#include <iostream> #include <cstdio> #include <string.h>using namespace Std;const int N=10010;const int M=35;char Html[m][n],stored[m][n],sta1[n][m];char mapping[m][m][2][m];int mapnum[m],sta2[n];void getHTML (int N) { int j,i=0,flag=1; Char Begintag[m],tag[m]; GetChar (); while (1) {Html[n][i]=getchar (); if (html[n][i]== ' < ') {j=0; while (Html[n][++i]=getchar ()) {if (html[n][i]== '/') continue; if (html[n][i]== ' | | | html[n][i]== ' > ') break; Tag[j++]=html[n][i]; } tag[j]= ' + '; if (flag==1) {strcpy (Begintag,tag); flag=0; } else if (!strcmp (Tag,begintag)) {html[n][++i]= ' + '; Return }} i++; }}void getmapping (int n,int m) {int i,j; Char mp[100]; cin>>mp; for (i=0; mp[i]!= '-'; i++)Mapping[n][m][0][i]=mp[i]; mapping[n][m][0][i]= ' + '; for (j=0,i++; I<strlen (MP); i++,j++) mapping[n][m][1][j]=mp[i]; mapping[n][m][1][j]= ' + ';} void Gettag (int n,int I,char tag[]) {int j=0; while (1) {i++; if (html[n][i]== '/') continue; if (html[n][i]== ' | | | html[n][i]== ' > ') break; Tag[j++]=html[n][i]; } tag[j]= ' + ';} int getId (int n,int I,char id[]) {int J; id[0]= ' + '; Char Tmp[m]; while (html[n][i]== ") {j=0; while (html[n][++i]!= ' = ') tmp[j++]=html[n][i]; tmp[j]= ' + '; if (!STRCMP (TMP, "id")) {i++; j=0; while (html[n][++i]!= ' "') id[j++]=html[n][i]; id[j]= ' + '; } else {i++; while (html[n][++i]!= ' "); } i++; } return i;} void Store (int n,int i,int J,char tag[]) {stored[j][0]= '; int K,y=0,flag=0,len=strlen (tag); for (i++;; i++) {k=0; if (html[n][i]== ' < ') for (; k<len; k++) if (tag[k]!=html[n][i+1+k]) break; if (K==len) flag++; k=0; if (html[n][i]== ' < ' &&html[n][i+1]== '/') for (; k<len; k++) if (Tag[k]!=html[n][i+2+k] ) break; if (K==len) {if (!flag) {stored[j][y]= ' + '; Return } else flag--; } Stored[j][y++]=html[n][i]; }}bool isstructure (int n,int m) {int i,j,k,ii,flag=0,top=-1; Char Tag[m],id[m],tag2[m],id2[m]; int Len1=strlen (html[n]); for (i=k=0; i<len1;) {ii=i; while (html[n][i]== ' | | html[n][i]== ' \ n ') i++; while (html[m][k]!= ' < ') k++; Gettag (N,i,tag); Gettag (M,K,TAG2); if (strcmp (TAG,TAG2) | | Html[n][i+1]!=html[m][k+1]) {if (!strcmp (TAG,TAG2)) sta2[top]++; if (!flag) {return false; } while(html[m][k]!= ' > ') k++; I=ii; Continue } if (html[n][i+1]== '/')//</xx> {if (!sta2[top]) {I+=strlen (tag) +3; flag--; } else sta2[top]--; K+=strlen (tag) +3; } else//<xx> or <xx/> {i+=strlen (tag) +1; K+=strlen (TAG2) +1; if (html[n][i]== ')//has ID {if (html[m][k]!= ') {if (!flag) {return false; } while (html[m][k]!= ' > ') k++; I=ii; Continue } i=getid (N,i,id); K=getid (M,K,ID2); if (strcmp (ID,ID2)) {if (!flag) {return false; } while (html[m][k]!= ' ≫ ') k++; I=ii; Continue }} for (j=0; j<mapnum[n]; j + +) if (!strcmp (id,mapping[n][j][0])) Break if (html[n][i]== '/')//<xx/> {i+=2; k+=2; } else//<xx> {if (J!=mapnum[n])//ID to be mapped { strcpy (Sta1[++top],tag); flag++; sta2[top]=0; for (j=0; j<mapnum[n]; j + +) if (!strcmp (id,mapping[n][j][0)) store (m,k , J,tag); } i++; k++; }}} return true; void output (int n) {int i,j,k,ii; Char Tag[m]; int Len1=strlen (html[0]); for (i=0; i<len1;) {while (i<len1&&html[0][i]!= ' < ') Putchar (html[0][i++]); if (I==LEN1) break; Gettag (0,i,tag); for (j=0; j<mapnum[n]; j + +) if (!strcmp (tag,mapping[n][j][1])) break; if (J==mapnum[n]) {Putchar (html[0][i++]); Continue } else {int len=strlen (tag); Ii=i; for (i+=len+1;; i++) {k=0; if (html[0][i]== ' < ' &&html[0][i+1]== '/') for (; k<len; k++) if (Tag[k] !=HTML[0][I+2+K]) break; if (K==len) break; } while (html[0][ii]!= ' > ') Putchar (html[0][ii++]); Putchar (html[0][ii++]); cout<<stored[j]; while (html[0][i]!= ' > ') Putchar (html[0][i++]); Putchar (html[0][i++]); }}}int Main () {//freopen ("data.in", "R", stdin); Freopen ("Data.out", "w", stdout); int t,ca=1; scanf ("%d", &t); while (t--) {int i,j,n,m; Gethtml (0); scanf ("%d", &n); for (I=1; i<=n; i++) {gethtml (i); scanf ("%d", &mapnum[i]); for (j=0; j<mapnum[i]; j + +) getmapping (I,J); } gethtml (n+1); printf ("Case #%d:\n", ca++); for (I=1; i<=n; i++) if (Isstructure (i,n+1)) {output (i); Break } if (i==n+1) printf ("Can ' t identify\n"); else Putchar (' \ n '); } return 0;}
HTML Markup Language processing HDU 4868