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

zoj 2907 Corporate Identity

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

zoj 2907 Corporate Identity

#include <vector>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct SubStr{    const char* str;    int id;    SubStr(const char *p, int i)        : str(p), id(i)    {    }    bool operator<(const SubStr& rhs) const    {        return strcmp(str, rhs.str) < 0;    }};int get_lcs(const char *a, const char *b, char *c){    int l = 0;    while (*a != '' && *a == *b) {        *c = *a;        ++l;        ++a;        ++b;        ++c;    }    *c = '';    return l;}static char str[4000][256], lcs[256], ans[256];int main(void){    int n, cmp, len;    while (scanf("%d", &n) != EOF && n != 0) {        vector<SubStr> v;        for (int i = 0; i < n; i++) { scanf("%s", str[i]); for (char *p = str[i]; *p != ''; p++) {     v.push_back(SubStr(p, i)); }        }        sort(v.begin(), v.end());        int cc = 0;        vector<int> c(n, 0);        vector<SubStr>::const_iterator i = v.begin(), j = v.begin();        strcpy(ans, "");        len = 0;        while (true) { while (j != v.end()) {     if (c[j->id] == 0) {         ++cc;     }     ++c[j->id];     if (cc == n) {         break;     }     else {         ++j;     } } if (j == v.end()) {     break; } while (c[i->id] > 1) {     --c[i->id];     ++i; } cmp = get_lcs(i->str, j->str, lcs); if (cmp > len) {     len = cmp;     strcpy(ans, lcs); } else if (cmp == len && strcmp(lcs, ans) < 0) {     strcmp(ans, lcs); } --cc; --c[i->id]; ++i; ++j;        };        if (strcmp(ans, "") == 0) { puts("IDENTITY LOST");        }        else { puts(ans);        }    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377672.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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