栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1044 Index Generation

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1044 Index Generation

#include<iostream>#include<cstring>#include<string>#include<map>#include<set>#include<stdio.h>using namespace std;enum {    SIZ = 104,};struct cmp{    bool operator()(const string&a, const string&b){        return strcasecmp(a.c_str(), b.c_str()) < 0;    }};char buf[SIZ];map<string, map<string, set<int>, cmp>, cmp> tab;const char *strBlank = " tnr";char *trim(char *s){    int len = strlen(s);    while(len && isblank(s[len-1])){        s[--len] = 0;    }    while(isblank(*s))        ++s;    return s;}void work(char *s, int pageNo){    char *p = index(s,'%');    if (p)        s = p + 1;    p = index(s, '$');    if (p){        *p++ = 0;    }    s = trim(s);    if (p){        p = trim(p);    }else{        p = "";    }    string a(s);    string b(p);    tab[a][b].insert(pageNo);}void parse(const char *str){    tab.clear();    int pageNo = 1;    while(*str){        if (*str=='&'){ ++pageNo;        }        if(*str=='{'){ ++str; int i=0; while(*str!='}'){     buf[i++] = *str;     ++str; } buf[i] = 0; work(buf, pageNo);        }        ++str;    }}bool fun(){    string s;    gets(buf);    if (strcmp(buf,"**")==0)         return false;    while(strcmp(buf,"*")!=0){        s += buf;        gets(buf);    }    parse(s.c_str());    return true;}void outSet(set<int> &s){    for(set<int>::iterator it=s.begin(); it!=s.end(); ++it){        printf(", %d", *it);    }}void outstr(const string&s, map<string, set<int>, cmp> &item){    map<string, set<int>, cmp>::iterator it;    printf ("%s", s.c_str());    bool f = true;    for (it=item.begin(); it!=item.end(); ++it){        if (it->first==""){ outSet(it->second); continue;        }        printf("n+ %s", it->first.c_str());        outSet(it->second);        f = false;    }    printf("n");}void output(){    map<string, map<string, set<int>, cmp>, cmp>::iterator it;    for (it=tab.begin(); it!=tab.end(); ++it){        outstr(it->first, it->second);    }}int main(){    int doc = 0;    for(;fun();){        printf("document %dn", ++doc);        output();    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/371079.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号