zoukankan      html  css  js  c++  java
  • poj2138

    排序

    View Code
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    using namespace std;

    #define maxn 1005
    #define maxl 100

    struct Word
    {
    int id;
    int len;
    }word[maxn];

    int n, m;
    char st[maxn][maxl];
    bool vis[maxn];
    bool found;

    bool operator < (const Word &a, const Word &b)
    {
    return a.len < b.len;
    }

    void input()
    {
    scanf("%d", &n);
    scanf("%s", st[n]);
    m = 0;
    int len1 = strlen(st[n]);
    found = false;
    for (int i = 0; i < n; i++)
    {
    scanf("%s", st[i]);
    if (strcmp(st[n], st[i]) == 0)
    found = true;
    word[m].id = i;
    word[m].len = strlen(st[i]);
    if (word[m].len > len1)
    m++;
    }
    if (!found)
    abort();
    word[m].len = len1;
    word[m].id = n;
    m++;
    }

    bool match(char *st1, char *st2, int len)
    {
    int l = 0;
    while (l < len && st1[l] == st2[l])
    l++;
    int r = len;
    while (r > 0 && st1[r - 1] == st2[r])
    r--;
    return l >= r;
    }

    void work()
    {
    memset(vis, 0, sizeof(vis));
    vis[0] = true;
    for (int i = 0; i < m; i++)
    if (vis[i])
    for (int j = i + 1; j < m && word[j].len <= word[i].len + 1; j++)
    if (!vis[j] && word[j].len == word[i].len + 1 && match(st[word[i].id], st[word[j].id], word[i].len))
    vis[j] = true;
    int i = m - 1;
    while (!vis[i])
    i--;
    printf("%s\n", st[word[i].id]);
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    input();
    sort(word, word + m);
    work();
    return 0;
    }

  • 相关阅读:
    吃货联盟项目
    字串符笔记
    带有参的方法
    js:自动亮起100盏灯
    JS字面量创建方式的优缺点
    为什么说对象字面量赋值比new Object()高效?
    javascript 字面量
    vue学习(一)、Vue.js简介
    Redis(二):c#连接Redis
    Redis(一):centos下安装。
  • 原文地址:https://www.cnblogs.com/rainydays/p/2197177.html
Copyright © 2011-2022 走看看