• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            算法學社
            記錄難忘的征途
            posts - 141,comments - 220,trackbacks - 0
            題目描述:
               給你一段XML程序,問它是否well-formed

            算法分析:
               用棧來模擬,判斷標簽是否匹配。
               trick是<a><b><a></a></b></a>是不可以的。

             1 #include<iostream>
             2 #include<cstdio>
             3 #include<cstring>
             4 #include<string>
             5 using namespace std;
             6 const int N = 10005;
             7 string ch;
             8 string stk[N];
             9 string tmp[N];
            10 const string start = "?xml version=\"1.0\"?";
            11 const string end = "?end?";
            12 int main(){
            13     char c;
            14     int back = 0, tp = 0;
            15     bool flag = 1, all_start = 1,begin = 1;
            16     while(c = getchar()) {
            17 //        cout<<c;
            18         if(c == '\n'continue;
            19         else if(c == '<') {
            20             back ++;
            21             ch.clear();
            22         } else if(c == '>') {
            23             back --;
            24             if(back != 0) flag = 0;
            25             back = 0;
            26             if(start == ch || ch == end) {
            27                 if(all_start) {
            28                     all_start = 0;
            29                 } else if(!flag || tp != 0) {
            30                     cout<< "non well-formed" << endl;
            31                 } else {
            32                     cout<< "well-formed" << endl;
            33                 }
            34                 begin = 1; tp = 0; flag = 1;
            35                 if(ch == end) break;
            36             } else if(ch[0== '/') {
            37                 ch.erase(ch.begin());
            38                 if(tp == 0 || ch != stk[tp-1]) flag = 0;
            39                 tp --;
            40             } else {
            41                 if(begin) begin = 0else if(tp == 0) flag = 0;
            42                 string name;
            43                 int n = ch.size(), len = 0;
            44                 if(ch[n-1]=='/'continue;
            45                 bool first = 1;
            46                 for(int i = 0; i <= n; i++) {
            47                     if(i == n || ch[i] == ' '){
            48                         if(name == "" || i && ch[i-1]==' 'continue;
            49                         //cout<<name<<" ";
            50                         if(first) {
            51                             for(int j = 0; j < tp ; j++)
            52                                 if(stk[j] == name) flag = 0;
            53                             stk[tp++= name;
            54                             first = 0;
            55                         } else {
            56                             int m = name.size();
            57                             int pos = (int)name.find('=');
            58                             //cout<<pos<<" ";
            59                             if(pos == -1) flag = 0
            60                             else {
            61                                 if(name[m-1== '"' && name[pos + 1== '"') {
            62                                     string temp = name.substr(0,pos);
            63                                     //cout<<temp;
            64                                     if(temp == "") flag = 0;
            65                                     else {
            66                                         for(int j = 0; j < len; j++)
            67                                             if(tmp[j] == temp) flag = 0;
            68                                         tmp[len ++= temp;
            69                                     }
            70                                 } else flag = 0;
            71                             }
            72                         }
            73                         name.clear();
            74                     } else name.push_back(ch[i]);
            75                 }
            76             }
            77         } else {
            78             if(back) ch.push_back(c);
            79             else if (tp == 0){
            80                 flag = 0;
            81             }
            82         }
            83 //        cout<<flag;//<<tp;
            84     }
            85 }
            86 
            posted on 2012-10-28 15:58 西月弦 閱讀(279) 評論(0)  編輯 收藏 引用 所屬分類: 解題報告
            久久亚洲国产中v天仙www| 一级做a爰片久久毛片看看| 久久精品天天中文字幕人妻| 久久国产精品无码一区二区三区| 色88久久久久高潮综合影院| 97精品国产97久久久久久免费| 武侠古典久久婷婷狼人伊人| 久久91精品久久91综合| 久久免费看黄a级毛片| 久久综合九色综合久99| 欧美黑人激情性久久| 99久久国产免费福利| 久久综合给合久久狠狠狠97色69| 伊人久久综在合线亚洲2019| 亚洲人成精品久久久久| 久久夜色精品国产| 久久亚洲精品视频| 成人综合伊人五月婷久久| 伊人久久大香线蕉AV色婷婷色| 久久综合狠狠综合久久97色| 精品一区二区久久| 69久久精品无码一区二区| 日产精品久久久久久久性色| 久久精品国产久精国产一老狼| 久久亚洲高清综合| 国产亚洲色婷婷久久99精品91| 久久国产精品一国产精品金尊 | 久久人做人爽一区二区三区| 国产综合精品久久亚洲| 久久se这里只有精品| 99久久精品国产一区二区蜜芽| 蜜桃麻豆www久久| 亚洲国产成人久久精品动漫| 99久久精品免费看国产| 国产精品99久久久久久猫咪| 久久久91人妻无码精品蜜桃HD| 久久成人18免费网站| 久久久久久亚洲精品影院| 中文字幕亚洲综合久久菠萝蜜| 久久精品日日躁夜夜躁欧美| 中文精品久久久久人妻不卡|