zoukankan      html  css  js  c++  java
  • 【u233】单词化简

    Time Limit: 1 second
    Memory Limit: 64 MB

    【问题描述】

    最近情报人员得到了一些经过加密的文章,每个单词都很长。破译人员想到先把单词化简一下,方法是把每个单词尽量取短些的前缀,但所取的前缀不能是其他单词的前缀。 这个任务现在就交给你来完成。 解释:“字符串s1是s2的前缀”是说把字符串s2的后面去掉某些,只保留与s1相同长度是,s2就与s1完全相同。如:“abc“是”abcaade“和”abc“的前缀,但不是”abadc“的前缀。 数据范围 单词数N,1<=n<=50; 每个单词长度不超过50;并且都是由小写字母构成。 保证所给单词没有一个单词是另一个单词的前缀。

    【输入格式】

    第一行一个整数N,表示单词的个数。 下面有N行,每行一个单词。

    【输出格式】

    共N行,每行一个单词,是对应上面N个单词化简后的单词。

    【数据规模】

    Sample Input1

    3
    abc
    efg
    ijh

    Sample Output1

    a
    e
    i

    Sample Input2

    3
    aac
    aad
    aae

    Sample Output2

    aac
    aad
    aae

    【题目链接】:http://noi.qz5z.com/viewtask.asp?id=u233

    【题解】

    如果s1比s2长,s1不是s2的前缀;
    枚举字符的前缀是什么(从短到长);然后看看是不是其他的字符串的前缀;

    【完整代码】

    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <set>
    #include <map>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <stack>
    #include <string>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    void rel(LL &r)
    {
        r = 0;
        char t = getchar();
        while (!isdigit(t) && t!='-') t = getchar();
        LL sign = 1;
        if (t == '-')sign = -1;
        while (!isdigit(t)) t = getchar();
        while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
        r = r*sign;
    }
    
    void rei(int &r)
    {
        r = 0;
        char t = getchar();
        while (!isdigit(t)&&t!='-') t = getchar();
        int sign = 1;
        if (t == '-')sign = -1;
        while (!isdigit(t)) t = getchar();
        while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
        r = r*sign;
    }
    
    const int MAXN = 50+10;
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    
    string s[MAXN];
    int n;
    
    bool f(string s1,string s2)
    {
        int len1=s1.size(),len2 = s2.size();
        if (len1>len2)
            return false;
        len1 = min(len1,len2);
        rep1(i,0,len1-1)
            if (s1[i]!=s2[i])
                return false;
        return true;
    }
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        scanf("%d",&n);
        rep1(i,1,n)
            cin >> s[i];
        rep1(i,1,n)
        {
            int len = s[i].size();
            int k = 1;
            while (k <= len)
            {
                string s1 = s[i].substr(0,k);
                bool is = false;
                rep1(j,1,n)
                    if (i!=j)
                    {
                        if (f(s1,s[j]))
                        {
                            is = true;
                            break;
                        }
                    }
                if (!is)
                {
                    cout << s1<<endl;
                    break;
                }
                k++;
            }
        }
        return 0;
    }
    
  • 相关阅读:
    html基础知识
    Python yield 使用浅析
    XSS跨站攻击
    box-sizing的用法(笔记)
    编译原理中DFA最小化
    提醒自己!!!
    Eclipse下运行maven项目失败且Tomcat服务器也启动不了
    Descriptio Resource Path LocationType Archive for required library: 'D:/apache-maven/apache-maven-3.6.0/mavenrepository/org/springframework/spring-aspects/4.3.7.RELEASE/spring-aspects-4.3.7.RELEASE.
    Eclispe创建maven工程缺失web.xml报web.xml is missing and <failOnMissingWebXml> is set to true的错误
    ssm 出现 Method threw 'org.apache.ibatis.binding.BindingException' exception.Invalid bound statement (not found)……
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626902.html
Copyright © 2011-2022 走看看