Airport Announcements URAL - 1889

题目描述:这哥们在机场听公告,假设机场说的公告每种语言含有的短语数是相同的,他有的词能听出什么语言,有的词听不出就是“unknown”,问一共有多少种语言,多种满足情况按照从大到下的顺序输出。
解题分析:简单模拟就好,没什么难度。

代码如下:

#include <cstdio>
#include <string>
#include <iostream>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
using namespace std;
vector <string> phrase;
int n;
set <int> ans;

bool solve(int m)
{
    set <string> lang;
    for(int i=0; i<n; i+=m)
    {
        string  cur;
        for(int j=0; j<m; j++)
        {
            if(phrase[i+j]!="unknown")
            {
                if(cur.empty())
                {
                    cur=phrase[i+j];
                }
                else
                {
                    if(cur!=phrase[i+j]) return false;
                }
            }
        }
        if(!cur.empty())
        {
            if(!lang.count(cur))
            {
                lang.insert(cur);
            }
            else
            {
                return false;
            }
        }
    }
    return true;
}

int main()
{
    string str;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        cin >> str;
        phrase.push_back(str);
    }
    for(int i=1; i<=n; i++)
    {
        if(n%i==0)
            if(solve(i)) ans.insert(n/i);
    }
    if(ans.empty()) cout << "Igor is wrong." << endl;
    else
    {
        set <int >::iterator it;
        for(it=ans.begin();it!=ans.end();it++)
        {
            if(it==ans.begin()) cout << *it;
            else cout << " " << *it ;
        }
        cout << endl;
    }

    return 0;
}
//这是当初彬佬所写,为了省时间给每个字符串编了个号,但实际上这题数据很少,不耗时间,下面是不编号的代码,和上面的时间相同。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
using namespace std;
vector <string> phrase;
int n;
set <int> ans;

bool solve(int m)
{
    set <string> lang;
    for(int i=0; i<n; i+=m)
    {
        string  cur;
        for(int j=0; j<m; j++)
        {
            if(phrase[i+j]!="unknown")
            {
                if(cur.empty())
                {
                    cur=phrase[i+j];
                }
                else
                {
                    if(cur!=phrase[i+j]) return false;
                }
            }
        }
        if(!cur.empty())
        {
            if(!lang.count(cur))
            {
                lang.insert(cur);
            }
            else
            {
                return false;
            }
        }
    }
    return true;
}

int main()
{
    string str;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        cin >> str;
        phrase.push_back(str);
    }
    for(int i=1; i<=n; i++)
    {
        if(n%i==0)
            if(solve(i)) ans.insert(n/i);
    }
    if(ans.empty()) cout << "Igor is wrong." << endl;
    else
    {
        set <int >::iterator it;
        for(it=ans.begin();it!=ans.end();it++)
        {
            if(it==ans.begin()) cout << *it;
            else cout << " " << *it ;
        }
        cout << endl;
    }

    return 0;
}



全部评论

相关推荐

2024-12-26 20:46
复旦大学 C++
国棉17厂丶小王:拿了offer的那个周末晚上去网吧通宵,去网吧不知道玩什么刷了lc的每日一题,然后试着第一次打开了三角洲行动,从此少了一个已经刷了700道题的lc用户,但是烽火地带多了一只🐭🐭
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务