前后缀分解+贪心,CF 762C - Two strings

目录

一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

二、解题报告

1、思路分析

2、复杂度

3、代码详解


一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

762C - Two strings


二、解题报告

1、思路分析

考虑合法方案:

去除掉b的一个子串后,b剩下的前缀和后缀一定分别是a的前缀和后缀的子序列

我们前后缀分离:

构建长度为 n 的两个数组 pre 和 suf

suf[i] 代表 a[i::] 能包含最长的 b[suf[i]::]

pre[] 代表 a[:i] 能包含的最长的 b[:pre[i]]

那么我们需要删除的最短长度就是 min{ suf[i + 1] - pre[i] - 1 }

本题要输出方案,那么维护下前缀长度和后缀起始位置即可

2、复杂度

时间复杂度: O(N)空间复杂度:O(N)

3、代码详解

 ​
#include <bits/stdc++.h>

// #define DEBUG

using u32 = unsigned;
using i64 = long long;
using u64 = unsigned long long;

constexpr int P = 1E9 + 7;
constexpr int inf32 = 1E9 + 7;
constexpr i64 inf64 = 1E18 + 7;

void solve() {
    std::string a, b;

    std::cin >> a >> b;

    int n = a.size(), m = b.size();

    std::vector<int> suf(n + 1, m);

    for (int i = n - 1, j = m - 1; ~i; -- i) {
        if (a[i] == b[j]) {
            -- j;
        }
        if (j < 0) {
            std::cout << b << '\n';
            return;
        }
        suf[i] = j + 1;
    }

    int mi = suf[0], mii = -1, len = 0;

    for (int i = 0, j = 0; i < n; ++ i) {
        if (a[i] == b[j]) {
            ++ j;
            if (mi > suf[i + 1] - j)
                mi = suf[i + 1] - j, mii = i, len = j;
        }
    }

    if (mi == m) {
        std::cout << "-\n";
    }
    else {
        std::cout << b.substr(0, len);
        if (suf[mii + 1] < m)
            std::cout << b.substr(suf[mii + 1]) << '\n';
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

#ifdef DEBUG
    int cur = clock();
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif

    int t = 1;
    // std::cin >> t;

    while (t--) {
        solve();
    }
#ifdef DEBUG
    std::cerr << "run-time: " << clock() - cur << '\n';
#endif
    return 0;
}

Add a Library class with the following UML specification: +-------------------------------------------+ | Library | +-------------------------------------------+ | - name: String | | - users: ArrayList | +-------------------------------------------+ | + Library(String name) | | + addUser(IUser user): void | | + totalBorrowedBooks(): int | | + getBook(String name): int | | + moreBook(String name, int number): void | | + testLibrary(): void | +-------------------------------------------+ When a library is created, it has an arraylist of users (IUser) but the arraylist is empty (the arraylist does not contain any user). The addUser method takes a user (IUser) as argument and adds the user to the arraylist of users for the library. The totalBorrowedBooks method returns as result the total number of books borrowed by all users of the library (the result can be either positive or negative). The getBook method takes as argument the name of a user and returns as result the number of books currently borrowed by the user. If the library does not have a user with the given name, then the getBook method must throw an UnknownUserException with the message "User XXX unknown.", where XXX is replaced with the name of the user. Do not worry about multiple users having the same name. You can assume all user names are unique in the arraylist. The moreBook method takes as argument the name of a user and a number of books and changes the number of books currently borrowed by that user. If the library does not have a user with the given name, then the moreBook method must throw an UnknownUserException with the message "User XXX unknown.", where XXX is replaced with the name of the user. Do not worry about multiple users having the same name. Note: the moreBook method does not catch any exception, it only throws exceptions. Hint: use the equals method to compare strings, not the == operator which only works with constant strings. 写java文件
05-25
下面是实现上述UML规范的Java代码: ```java import java.util.ArrayList; public class Library { private String name; private ArrayList<IUser> users; public Library(String name) { this.name = name; users = new ArrayList<IUser>(); } public void addUser(IUser user) { users.add(user); } public int totalBorrowedBooks() { int total = 0; for (IUser user : users) { total += user.getBorrowedBooks(); } return total; } public int getBook(String name) throws UnknownUserException { for (IUser user : users) { if (user.getName().equals(name)) { return user.getBorrowedBooks(); } } throw new UnknownUserException("User " + name + " unknown."); } public void moreBook(String name, int number) throws UnknownUserException { for (IUser user : users) { if (user.getName().equals(name)) { user.setBorrowedBooks(user.getBorrowedBooks() + number); return; } } throw new UnknownUserException("User " + name + " unknown."); } public void testLibrary() { IUser user1 = new User("Alice"); IUser user2 = new User("Bob"); addUser(user1); addUser(user2); try { System.out.println(getBook("Alice")); // should print 0 moreBook("Bob", 2); System.out.println(getBook("Bob")); // should print 2 moreBook("Charlie", 1); // should throw an UnknownUserException } catch (UnknownUserException e) { System.out.println(e.getMessage()); } System.out.println(totalBorrowedBooks()); // should print 2 } } ``` 在上面的代码中,我们定义了一个`Library`类,用于管理图书馆的用户和借阅信息。该类有一个私有成员变量`name`,表示图书馆的名称,还有一个私有成员变量`users`,表示图书馆的用户列表。我们提供了一个构造方法`Library(String name)`,用于创建一个新的图书馆对象。 我们还提供了以下公共方法: - `void addUser(IUser user)`:将一个新用户添加到图书馆的用户列表中。 - `int totalBorrowedBooks()`:返回当所有用户已借阅的书籍总数。 - `int getBook(String name)`:返回指定用户当已借阅的书籍数。如果图书馆中不存在该用户,则抛出`UnknownUserException`异常。 - `void moreBook(String name, int number)`:将指定用户已借阅的书籍数增加`number`。如果图书馆中不存在该用户,则抛出`UnknownUserException`异常。 我们还提供了一个`testLibrary`方法,用于测试`Library`类中的各个方法是否正常工作。 注意,在`getBook`和`moreBook`方法中,我们使用了`equals`方法来比较字符串是否相等,而不是使用`==`运算符。这是因为`==`运算符只能用于比较常量字符串,而不能用于比较变量中存储的字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EQUINOX1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值