日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

HDU 4868 Information Extraction(2014 多校联合第一场 H)

發(fā)布時(shí)間:2023/12/19 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 4868 Information Extraction(2014 多校联合第一场 H) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

看到這道題時(shí)我的內(nèi)心是奔潰的,沒有了解過(guò)HTML,只能靠窩的渣渣英語(yǔ)一點(diǎn)一點(diǎn)翻譯啊TT、

Information Extraction

題意:(純手工翻譯,有些用詞可能在html中不是一樣的,還多包涵)
從HTML文檔中提取信息,用一種特殊的格式輸出。
HTML文件的定義如下:
HTML:
?? 是一種超文本標(biāo)記語(yǔ)言。標(biāo)記語(yǔ)言是由一系列的標(biāo)記組成的。

 標(biāo)簽描述文檔內(nèi)容。HTML文件由標(biāo)簽和文本組成。
標(biāo)簽:
?? HTML使用標(biāo)簽來(lái)實(shí)現(xiàn)他的語(yǔ)法。

?? 標(biāo)簽由特殊的字符(如: ‘<’, ‘>’ and ‘/’)組成。

?? 標(biāo)簽通常成對(duì)出現(xiàn),起始標(biāo)簽和結(jié)束標(biāo)簽。起始標(biāo)簽以<開頭,>結(jié)尾。

?? 結(jié)束標(biāo)簽以</開頭,以>結(jié)尾。文件的其他地方不會(huì)出現(xiàn)尖括號(hào)。

?? 標(biāo)簽名是只含有小寫字母的字符串。標(biāo)簽中沒有行中斷。

?? 除了標(biāo)簽,文件中出現(xiàn)的其他東西都被認(rèn)為是文本內(nèi)容。

?? 標(biāo)簽名的長(zhǎng)度不超過(guò)30.
元素:
?? 元素是起始標(biāo)簽和相對(duì)應(yīng)的終止標(biāo)簽之間的任何內(nèi)容,包括標(biāo)簽在內(nèi)。

?? 元素內(nèi)容是開始標(biāo)簽和結(jié)束標(biāo)簽之間的內(nèi)容。

?? 有些元素沒有內(nèi)容,我們叫它空元素,如 <hr></hr>。

?? 空元素可以被關(guān)在一個(gè)打開標(biāo)簽內(nèi),以/>結(jié)尾,而不是>。

?? 所有元素都可以被關(guān)閉用關(guān)閉標(biāo)簽或者在開始標(biāo)簽里面。

?? 元素可以有屬性。

?? 元素可以被嵌套,即一個(gè)元素可以包含另一個(gè)元素。

?? <html>元素師其他所有元素的容器,他不包含任何屬性。
屬性:
?? 屬性為元素提供額外信息。

?? 屬性總是在開始標(biāo)簽中被規(guī)定,在標(biāo)簽名的后面。

?? 標(biāo)簽名和屬性由一個(gè)空格隔開。

?? 一個(gè)元素可以有多個(gè)屬性。

?? 屬性以這樣的形式出現(xiàn):name="value" class="icpc",等號(hào)兩邊沒有空格。

?? 所有的屬性名都是小寫的。

?? id屬性的值是唯一的,長(zhǎng)度小于等于30.

?

題目中還給了幾個(gè)例子,我們來(lái)看一下:

sample 1:?

<html><body>? //<html>是總?cè)萜?#xff0c;<body>是標(biāo)簽
<h3 id="header" class="style1">this is a test</h3>?? //<h3 id="header" class="style1">是起始標(biāo)簽,標(biāo)簽名是h3,有兩個(gè)屬性,id和class?!皌his is a test”是元素。</h3>是結(jié)束標(biāo)簽。
<div id="content" class="style2">?? //<div> 標(biāo)簽,同上。
this is content<br/>
<pre>var x = 1111; </pre>
</div>
</body></html>? //結(jié)束標(biāo)簽

?

由結(jié)構(gòu)到輸出格式的映射如下:

id屬性的值-輸出格式的標(biāo)簽名

?

輸入:

T:代表有幾組樣例。

每一個(gè)樣例都在前面規(guī)定它的輸出格式。

n:有幾種html文件

每一種類型的l結(jié)構(gòu)在之前的html文件中

m: 有幾種映射從結(jié)構(gòu)到輸出結(jié)果

m行映射

最后是html測(cè)試案例

?

輸出:

每一組樣例,第一行輸出“Case #x”

如果存在這種結(jié)構(gòu)的html文件,按格式輸出,否則,輸出“Can't Identify”如果有多種結(jié)構(gòu),使用最早輸入的結(jié)構(gòu)。

?

下面我們來(lái)分析一樣題目給出的測(cè)試數(shù)據(jù):

input:

2 <news> <title>default title</title> <content width="1000px"></content> </news> 1 <html><h3 id="header"></h3> <div id="content"></div></html> 2 header-title content-content <html><h3 id="header" class="style1"> this is a test</h3> <div id="content" class="style2"> this is content<br/> <pre>var x = 1111;</pre> </div> </html> <xxx> <title>default title</title> </xxx> 1 <html><h3 id="header"></h3></html> 1 header-title <html><h3 id="tmp">xxxx</h3></html>

?

output:

Case #1: <news> <title> this is a test</title> <content width="1000px"> this is content<br/> <pre>var x = 1111;</pre> </content> </news>

Case #2: Can't Identify

?

輸入分析:

藍(lán)色部分規(guī)定了輸出格式,綠色部分規(guī)定了html文件的結(jié)構(gòu),紅色部分為m種映射,紫色部分是需要提取信息的文本。

?

拿case 1 來(lái)看,<html><h3 id="header" class="style1"> this is a test</h3> <div id="content" class="style2"> this is content<br/> <pre>var x = 1111;</pre> </div> </html>?? 從中我們可以得出的信息為 標(biāo)簽h3,它的屬性id的值為header,上述header的映射為title,所以用標(biāo)簽為title 的格式輸出“<title> this is a test</title>”? 接下來(lái)是div標(biāo)簽,它的屬性id的值為content,上述content的映射為content,所以用標(biāo)簽為content的格式輸出“?<content width="1000px"> this is content<br/> <pre>var x = 1111;</pre> </content>”,在前后分別加上<news></news>;

case 2:找不到屬性值為tmp的映射,所以不能定義。

?

題意,樣例都說(shuō)完了,下面來(lái)看看代碼吧(不是本人寫的):

?

1 #include<iostream> 2 #include<cstdio> 3 #include<string.h> 4 using namespace std; 5 const int N=10010; 6 const int M=35; 7 char html[M][N],stored[M][N],sta1[N][M]; 8 char mapping[M][M][2][M];//存儲(chǔ)映射情況 9 int mapNum[M],sta2[N]; 10 void getHtmlFormat(int n)//讀取html的格式 11 { 12 int j,i=0,flag=1; 13 char beginTag[M];//存儲(chǔ)開始標(biāo)簽 14 char tag[M];//存儲(chǔ)標(biāo)簽 15 getchar(); 16 while(1) 17 { 18 html[n][i]=getchar();//讀取第n個(gè)html 19 if(html[n][i]=='<') 20 { 21 j=0; 22 while(html[n][++i]=getchar()) 23 { 24 if(html[n][i]=='/') 25 continue; 26 if(html[n][i]==' '||html[n][i]=='>') 27 break; 28 tag[j++]=html[n][i]; 29 } 30 tag[j]='\0'; 31 if(flag==1) 32 { 33 strcpy(beginTag,tag); 34 flag=0; 35 } 36 else if(!strcmp(tag,beginTag))//表示讀到結(jié)束標(biāo)簽,讀取結(jié)束 37 { 38 html[n][++i]='\0'; 39 return; 40 } 41 } 42 i++; 43 } 44 } 45 void getMapping(int n,int m) 46 { 47 int i,j; 48 char mp[100]; 49 cin>>mp; 50 for(i=0; mp[i]!='-'; i++) 51 mapping[n][m][0][i]=mp[i]; 52 mapping[n][m][0][i]='\0'; 53 for(j=0,i++; i<strlen(mp); i++,j++) 54 mapping[n][m][1][j]=mp[i]; 55 mapping[n][m][1][j]='\0'; 56 } 57 void getTag(int n,int i,char tag[]) 58 { 59 int j=0; 60 while(1) 61 { 62 i++; 63 if(html[n][i]=='/') 64 continue; 65 if(html[n][i]==' '||html[n][i]=='>') 66 break; 67 tag[j++]=html[n][i]; 68 } 69 tag[j]='\0'; 70 } 71 int getId(int n,int i,char id[]) 72 { 73 int j; 74 id[0]='\0'; 75 char tmp[M]; 76 while(html[n][i]==' ') 77 { 78 j=0; 79 while(html[n][++i]!='=') 80 tmp[j++]=html[n][i]; 81 tmp[j]='\0'; 82 if(!strcmp(tmp,"id")) 83 { 84 i++; 85 j=0; 86 while(html[n][++i]!='"') 87 id[j++]=html[n][i]; 88 id[j]='\0'; 89 } 90 else 91 { 92 i++; 93 while(html[n][++i]!='"'); 94 } 95 i++; 96 } 97 return i; 98 } 99 void store(int n,int i,int j,char tag[]) 100 { 101 stored[j][0]='\0'; 102 int k,y=0,flag=0,len=strlen(tag); 103 for(i++;; i++) 104 { 105 k=0; 106 if(html[n][i]=='<') 107 for(; k<len; k++) 108 if(tag[k]!=html[n][i+1+k])break; 109 if(k==len)flag++; 110 k=0; 111 if(html[n][i]=='<'&&html[n][i+1]=='/') 112 for(; k<len; k++) 113 if(tag[k]!=html[n][i+2+k])break; 114 if(k==len) 115 { 116 if(!flag) 117 { 118 stored[j][y]='\0'; 119 return; 120 } 121 else flag--; 122 } 123 stored[j][y++]=html[n][i]; 124 } 125 } 126 bool isStructure(int n,int m) 127 { 128 int i,j,k,ii,flag=0,top=-1; 129 char tag[M],id[M],tag2[M],id2[M]; 130 int len1=strlen(html[n]); 131 for(i=k=0; i<len1;) 132 { 133 ii=i; 134 while(html[n][i]==' '||html[n][i]=='\n') 135 i++; 136 while(html[m][k]!='<') 137 k++; 138 getTag(n,i,tag);//獲取標(biāo)簽 139 getTag(m,k,tag2); 140 if(strcmp(tag,tag2)||html[n][i+1]!=html[m][k+1]) 141 { 142 if(!strcmp(tag,tag2)) 143 sta2[top]++; 144 if(!flag) 145 { 146 return false; 147 } 148 while(html[m][k]!='>') 149 k++; 150 i=ii; 151 continue; 152 } 153 if(html[n][i+1]=='/') //</xx> 154 { 155 if(!sta2[top]) 156 { 157 i+=strlen(tag)+3; 158 flag--; 159 } 160 else sta2[top]--; 161 k+=strlen(tag)+3; 162 } 163 else //<xx>或者<xx/> 164 { 165 i+=strlen(tag)+1; 166 k+=strlen(tag2)+1; 167 if(html[n][i]==' ') //有id 168 { 169 if(html[m][k]!=' ') 170 { 171 if(!flag) 172 { 173 return false; 174 } 175 while(html[m][k]!='>')k++; 176 i=ii; 177 continue; 178 } 179 i=getId(n,i,id); 180 k=getId(m,k,id2); 181 if(strcmp(id,id2)) 182 { 183 if(!flag) 184 { 185 return false; 186 } 187 while(html[m][k]!='>')k++; 188 i=ii; 189 continue; 190 } 191 } 192 for(j=0; j<mapNum[n]; j++) 193 if(!strcmp(id,mapping[n][j][0])) 194 break; 195 if(html[n][i]=='/') //<xx/> 196 { 197 i+=2; 198 k+=2; 199 } 200 else //<xx> 201 { 202 if(j!=mapNum[n]) //需映射的id 203 { 204 strcpy(sta1[++top],tag); 205 flag++; 206 sta2[top]=0; 207 for(j=0; j<mapNum[n]; j++) 208 if(!strcmp(id,mapping[n][j][0])) 209 store(m,k,j,tag); 210 } 211 i++; 212 k++; 213 } 214 } 215 } 216 return true; 217 } 218 void output(int n) 219 { 220 int i,j,k,ii; 221 char tag[M]; 222 int len1=strlen(html[0]); 223 for(i=0; i<len1;) 224 { 225 while(i<len1&&html[0][i]!='<') 226 putchar(html[0][i++]); 227 if(i==len1)break; 228 getTag(0,i,tag); 229 for(j=0; j<mapNum[n]; j++) 230 if(!strcmp(tag,mapping[n][j][1])) 231 break; 232 if(j==mapNum[n]) 233 { 234 putchar(html[0][i++]); 235 continue; 236 } 237 else 238 { 239 int len=strlen(tag); 240 ii=i; 241 for(i+=len+1;; i++) 242 { 243 k=0; 244 if(html[0][i]=='<'&&html[0][i+1]=='/') 245 for(; k<len; k++) 246 if(tag[k]!=html[0][i+2+k]) 247 break; 248 if(k==len) 249 break; 250 } 251 while(html[0][ii]!='>') 252 putchar(html[0][ii++]); 253 putchar(html[0][ii++]); 254 cout<<stored[j]; 255 while(html[0][i]!='>') 256 putchar(html[0][i++]); 257 putchar(html[0][i++]); 258 } 259 } 260 } 261 int main() 262 { 263 int T; 264 scanf("%d",&T); 265 for(int cases=1;cases<=T;cases++) 266 { 267 int i,j,n,m; 268 getHtmlFormat(0); 269 scanf("%d",&n); 270 for(i=1; i<=n; i++) 271 { 272 getHtmlFormat(i); 273 scanf("%d",&mapNum[i]); 274 for(j=0; j<mapNum[i]; j++) 275 getMapping(i,j); 276 } 277 getHtmlFormat(n+1);// 待處理的html文件 278 printf("Case #%d:\n",cases); 279 for(i=1; i<=n; i++) 280 if(isStructure(i,n+1)) 281 { 282 output(i); 283 break; 284 } 285 if(i==n+1) 286 printf("Can't Identify"); 287 putchar('\n'); 288 } 289 return 0; 290 }

?

轉(zhuǎn)載于:https://www.cnblogs.com/PJQOOO/p/4641185.html

總結(jié)

以上是生活随笔為你收集整理的HDU 4868 Information Extraction(2014 多校联合第一场 H)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。