离散题目12

这篇博客探讨了如何使用C++处理离散数学中的函数关系问题。通过getline()、string和stringstream来处理输入数据,作者分析了代码超时的原因,并提供了题目链接和示例输入输出。文章还包含了Time Limit Exceed和Accepted两种状态的代码示例。

think:
1getline()+ string + stringstream
2自己初始代码超时原因——此题目后台评测数据中可能出现输入结束后空格回车情况

sdut题目链接

离散题目12
Time Limit: 1000MS Memory Limit: 65536KB

Problem Description
给出两个集合,以及两个集合上的关系。判断该关系能不能构成函数

Input
多组输入。第一行数字表示集合A;第二行数字表示集合B;第三行一个数字N,表示关系的个数。以下N行,每行两个数字a b,用来描述关系a→b。0 < n < = 20000,集合A、B的大小不超过10000.

Output
每组数据输出一行,所给关系属于函数,输出’yes’ ,否则输出‘no’。

Example Input
1 2 3
4 5 6
3
1 4
2 5
3 6
1 2 3
4 5 6
3
1 4
1 5
1 6

Example Output
yes
no

Hint

Author

以下为Time limit exceed代码

#include <bits/stdc++.h>

using namespace std;

set<int> s1, s2;
string aaaa;

bool red();

int main()
{
    int n, x, y;
    while(red())
    {
        scanf("%d", &n);
        int flag = true;
        map<int, int> m;
        while(n--)
        {
            scanf("%d %d", &x, &y);
            if(s1.count(x) && s2.count(y))
            {
                if(m.count(x) != 0 && m[x] != y)
                    flag = false;
                else
                    m[x] = y;
            }
            else
                flag = false;
        }
        if(flag)
            printf("yes\n");
        else
            printf("no\n");
        //getline(cin, aaaa);
        ///getchar();//超时
    }
    return 0;
}

bool red()
{
    s1.clear(), s2.clear();
    string si;
    if(!getline(cin, si))
        return false;

    stringstream ss(si);
    int t;
    while(ss >> t)
    {
        s1.insert(t);
    }

    getline(cin, si);
    ss.clear();
    ss.str(si);
    while(ss >> t)
    {
        s2.insert(t);
    }
    return true;
}


/***************************************************
User name: 
Result: Time Limit Exceeded
Take time: 1010ms
Take Memory: 0KB
Submit time: 2017-04-03 21:29:05
****************************************************/

以下为Time limit exceed代码

#include <bits/stdc++.h>

using namespace std;

set<int> s1, s2;
string aaaa;

bool red();

int main()
{
    int n, x, y;
    while(red())
    {
        scanf("%d", &n);
        int flag = true;
        map<int, int> m;
        while(n--)
        {
            scanf("%d %d", &x, &y);
            if(s1.count(x) && s2.count(y))
            {
                if(m.count(x) != 0 && m[x] != y)
                    flag = false;
                else
                    m[x] = y;
            }
            else
                flag = false;
        }
        if(flag)
            printf("yes\n");
        else
            printf("no\n");
        //getline(cin, aaaa);
        getchar();
    }
    return 0;
}

bool red()
{
    s1.clear(), s2.clear();
    string si;
    if(!getline(cin, si))
        return false;

    stringstream ss(si);
    int t;
    while(ss >> t)
    {
        s1.insert(t);
    }

    getline(cin, si);
    ss.clear();
    ss.str(si);
    while(ss >> t)
    {
        s2.insert(t);
    }
    return true;
}


/***************************************************
User name: 
Result: Time Limit Exceeded
Take time: 1010ms
Take Memory: 0KB
Submit time: 2017-04-03 21:27:33
****************************************************/

以下为Accepted代码

#include <bits/stdc++.h>

using namespace std;

set<int> s1, s2;
string aaaa;

bool red();

int main()
{
    int n, x, y;
    while(red())
    {
        scanf("%d", &n);
        int flag = true;
        map<int, int> m;
        while(n--)
        {
            scanf("%d %d", &x, &y);
            if(s1.count(x) && s2.count(y))
            {
                if(m.count(x) != 0 && m[x] != y)
                    flag = false;
                else
                    m[x] = y;
            }
            else
                flag = false;
        }
        if(flag)
            printf("yes\n");
        else
            printf("no\n");
        getline(cin, aaaa);
        ///getchar();//超时
    }
    return 0;
}

bool red()
{
    s1.clear(), s2.clear();
    string si;
    if(!getline(cin, si))
        return false;

    stringstream ss(si);
    int t;
    while(ss >> t)
    {
        s1.insert(t);
    }

    getline(cin, si);
    ss.clear();
    ss.str(si);
    while(ss >> t)
    {
        s2.insert(t);
    }
    return true;
}


/***************************************************
User name: 
Result: Accepted
Take time: 52ms
Take Memory: 1336KB
Submit time: 2017-04-03 21:29:24
****************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值