D - Database Gym - 101308D STL的运用

该博客介绍了如何解决一个特定问题,即将字符串转换为二维数组并使用STL(Standard Template Library)来检查是否存在相同的行。题目要求找出两行两列中相同的字符串,如果没有则输出'no'及涉及的行号和列号。博主分享了利用STL灵活解决此问题的方法。



题意:

给出n行  m列字符串

请你找出某两行两列的相同

如果有不同就输出no  再输出两行的行数  再输出两列的列数

题解:

输入的时候先将字符串映射到一个数字保存成一个二维数组

然后枚举两列,判断哪两行满足题意

这里的stl用的很活   可以多看看


#include<iostream>
#include<algorithm>
#include<cstring>
#include<stdio.h>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<int,long long> pll;
const int INF = 0x3f3f3f3f;
const int maxn=100+5;

map<string, int> IDcache;
map<pair<int, int>, int> NewIDcache;
int a[11000][20];
int n, m;


void match()
{
    int x, y;
    for (int c1 = 0; c1 < m-1; c1++)
    {
        for (int c2 = c1 + 1; c2 < m; c2++)
        {
            for (int r = 0; r < n; r++)
            {
                x = a[r][c1];
                y = a[r][c2];
                if (!NewIDcache.count(make_pair(x, y)))  NewIDcache[make_pair(x, y)] = r;
                else
                {
                    cout << "NO" << endl;
                    cout << NewIDcache[make_pair(x, y)] + 1 << " " << r + 1 << endl;
                    cout << c1 + 1 << " " << c2 + 1 << endl;
                    return;
                }
            }
            NewIDcache.clear();
        }
    }
    cout << "YES" << endl;
}


int main()
{
    freopen("in.txt","r",stdin);
    //freopen("database.in","r",stdin);
    //freopen("database.out","w",stdout);
    while (cin >> n >> m)
    {
        int t = 1;
        getchar();
        string str;
        IDcache.clear();
        NewIDcache.clear();
        for (int i = 0; i < n; i++)
        {
            int count = 0;
            str.clear();
            getline(cin, str);
            int l = str.length();
            string s;
            for (int j = 0; j < l; j++)
            {
                if (str[j] != ',')  s = s + str[j];
                if (str[j] == ',' || j == l-1)
                {
                    if (!IDcache.count(s))  IDcache[s] = t++;
                    a[i][count++] = IDcache[s];
                    s.clear();
                }
            }
        }
        match();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值