cf 补题

本文介绍了如何使用O(log n)的时间复杂度解决两个问题:一是判断n个字符串是否能由其他字符串组成;二是计算旋转矩阵使其形状不变所需的最小翻转次数。通过巧妙的数据结构和算法策略,优化了原始的暴力搜索方法。
摘要由CSDN通过智能技术生成

Double Strings

题意

n个字符串,判断每个字符串是否能由其他字符串组成(可以两个相同的),可以输出1,不可以输出0。

思路

暴力的话如果遍历每个字符串并搜索是否能由两个字符串组成,时间复杂度是O(n^3) (不知道理解的对不对)。如果换一种方式搜索,将出现过的字符串记录用map记录下来,从头到尾遍历字符串,看将其分成两部分 s[0~j] s[j~size-1]是否出现过。这样复杂度是 O(l n longn)。l为字符串长度

#define _CRT_SECURE_NO_WARNINGS 1;
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>
using namespace std;
#define off ios::sync_with_stdio(false);
#define INF 0x3f3f3f3f
#define forn(i,n) for(int i = 0; i < int(n); i++)

const int N = 1e5 + 10;
const int M = 2e7 + 10;
const int mod = 1e9 + 7;
typedef pair<int, int> PII;
typedef long long ll;

string s[N];
map<string, bool> p;

int main()
{
    off;
    int t;
    cin >> t;
    while(t--) {
        p.clear();
        int n;
        cin >> n;
        forn(i, n) {
            cin >> s[i];
            p[s[i]] = true;
        }
        forn(i, n) {
            int f = 0;
            for (int j = 0; j < s[i].size(); j++) {
                string s1 = s[i].substr(0, j);
                string s2 = s[i].substr(j, s[i].size() - 1);
                if (p[s1] && p[s2]) {
                    f = 1;
                    break;
                }
            }
            cout << f;
        }
        cout << endl;
    }
    return 0;
}

Mirror Grid

题意

给出一个数字矩阵,可以将其中的1变0,0变1。判断使其旋转90,180,270形状一直不变的最小次数。

 思路

通过模拟发现矩阵应该形如这样

a  b  c  a

c  d  d  b

b  d  d  c

a  c  b  a 

假设当前坐标为 (i, j) 旋转之后应该为 (j, n - i - 1)。

#define _CRT_SECURE_NO_WARNINGS 1;
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>
using namespace std;
#define off ios::sync_with_stdio(false);
#define INF 0x3f3f3f3f
#define forn(i,n) for(int i = 0; i < int(n); i++)

const int N = 100 + 10;
const int M = 2e7 + 10;
const int mod = 1e9 + 7;
typedef pair<int, int> PII;
typedef long long ll;

int a[N][N];

int main()
{
    off;
    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                char c;
                cin >> c;
                a[i][j] = c - '0';
            }
        }
        int ans = 0;
        for (int i = 0; i < (n + 1) / 2; i++) {
            for (int j = 0; j < n / 2; j++) {
                int ii = i, jj = j;
                int resj = j;
                int cnt = 0;
                cnt += a[ii][jj];
                jj = n - ii - 1;
                ii = resj;
                resj = jj;
                cnt += a[ii][jj];
                jj = n - ii - 1;
                ii = resj;
                resj = jj;
                cnt += a[ii][jj];
                jj = n - ii - 1;
                ii = resj;
                resj = jj;
                cnt += a[ii][jj];
                ans += min(cnt, 4 - cnt);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值