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

poj 2162 Document Indexing

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

poj 2162 Document Indexing

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <queue>#include <cctype>#include <map>#include <cstring>#include <string>using namespace std;typedef long long LL;const int MAXN = 1010;map<string, int> mymap;char s[20000][MAXN];bool ans[20000][10000];int n, page, row, cur, cnt;string to_str(char *&st) {    while(!isalpha(*st) && *st != 0) ++st;    string ret;    while(isalpha(*st) && *st != 0) {        if(islower(*st)) *st += 'A' - 'a';        ret += *st, ++st;    }    return ret;}void to_map(char *s) {    string tmp;    while(true) {        tmp = to_str(s);        if(tmp == "") break;        int now;        if(mymap.find(tmp) != mymap.end()) now = mymap[tmp];        else mymap[tmp] = now = ++cnt;        //cout<<tmp<<endl;        ans[now][page] = true;    }}void output() {    map<string, int>::iterator it;    for(it = mymap.begin(); it != mymap.end(); ++it) {        bool flag = false;        int now = it->second;        cout<<it->first;        for(int i = 1; i <= page; ++i) { if(!ans[now][i]) continue; if(!flag) putchar(' '), flag = true; else putchar(','); printf("%d", i); int j = i; while(ans[now][j + 1]) ++j; if(j >= i + 2) {     printf("-%d", j);     i = j; }        }        puts("");    }}int main() {    scanf("%d", &n); getchar();    page = 1, row = 1;    cur = 1; cnt = 0;    bool flag = true;    mymap.clear();    while(flag && gets(s[0])) {        cur = 1;        while((flag = gets(s[cur])) && s[cur][0] != 0) ++cur;        if(cur == 1) { to_map(s[0]); ++row; if(++row > n) row = 1, ++page; continue;        }        if(cur == 2) { if(row == n) row = 1, ++page; to_map(s[0]); to_map(s[1]); row += 2; if(++row > n) row = 1, ++page; continue;        }        if(cur == 3) { if(row + 1 == n || row == n) row = 1, ++page; to_map(s[0]); to_map(s[1]); to_map(s[2]); row += 3; if(++row > n) row = 1, ++page; continue;        }        if(row == n) row = 1, ++page;//cur >= 4        for(int i = 0; i < cur; ++i) { if(row == n && i == cur - 2) row = 1, ++page; to_map(s[i]); ++row; if(row > n) row = 1, ++page;        }        if(row == 1) continue;        if(++row > n) row = 1, ++page;    }    output();}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/376150.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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