1039. Course List for Student (25)【排序】——PAT (Advanced Level) Practise

本文详细阐述了解决Zhejiang大学学生课程注册列表查询问题的算法实现,包括输入输出规范、数据处理策略及关键步骤解析。

题目信息

1039. Course List for Student (25)

时间限制200 ms
内存限制65536 kB
代码长度限制16000 B

Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists of all the courses, you are supposed to output the registered course list for each student who comes for a query.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers: N (<=40000), the number of students who look for their course lists, and K (<=2500), the total number of courses. Then the student name lists are given for the courses (numbered from 1 to K) in the following format: for each course i, first the course index i and the number of registered students Ni (<= 200) are given in a line. Then in the next line, Ni student names are given. A student name consists of 3 capital English letters plus a one-digit number. Finally the last line contains the N names of students who come for a query. All the names and numbers in a line are separated by a space.

Output Specification:

For each test case, print your results in N lines. Each line corresponds to one student, in the following format: first print the student’s name, then the total number of registered courses of that student, and finally the indices of the courses in increasing order. The query results must be printed in the same order as input. All the data in a line must be separated by a space, with no extra space at the end of the line.

Sample Input:
11 5
4 7
BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
1 4
ANN0 BOB5 JAY9 LOR6
2 7
ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR6
3 1
BOB5
5 9
AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6 NON9
Sample Output:
ZOE1 2 4 5
ANN0 3 1 2 5
BOB5 5 1 2 3 4 5
JOE4 1 2
JAY9 4 1 2 4 5
FRA8 3 2 4 5
DON2 2 4 5
AMY7 1 5
KAT3 3 2 4 5
LOR6 4 1 2 4 5
NON9 0

解题思路

排序,最后一个点会卡时,string会超时,hash后A掉(ps.排序我惯用set或map辅助,,,)

AC代码

#include <cstdio>
#include <vector>
#include <map>
#include <string>
#include <set>
using namespace std;
int main()
{
    int n, cn, cid, cidn, t;
    char s[5];
    map<int, set<int> > mp;
    scanf("%d%d", &n, &cn);
    for (int i = 0; i < cn; ++i){
        scanf("%d%d", &cid, &cidn);
        for (int j = 0; j < cidn; ++j){
            scanf("%s", s);
            t = (s[0]-'A')*26*26*10 + (s[1]-'A')*26*10 + (s[2]-'A')*10 + s[3]-'0';
            mp[t].insert(cid);
        }
    }
    for (int i = 0; i < n; ++i){
        scanf("%s", s);
        t = (s[0]-'A')*26*26*10 + (s[1]-'A')*26*10 + (s[2]-'A')*10 + s[3]-'0';
        printf("%s %d", s, mp[t].size());
        for (set<int>::iterator it = mp[t].begin(); it != mp[t].end(); ++it){
            printf(" %d", *it);
        }
        printf("\n");
    }
    return 0;
}
<think>好的,用户遇到了Spring Boot项目中UserMapper Bean未找到的问题。我需要先分析可能的原因,然后给出解决方案。首先,UserMapper通常是一个MyBatis的Mapper接口,Bean未找到可能是因为没有被正确扫描到。 首先考虑组件扫描的问题。Spring Boot需要在主应用类上使用@MapperScan注解来指定Mapper接口的包路径,或者每个Mapper接口添加@Mapper注解。如果用户没有正确配置这些,会导致Bean无法被创建。 接下来,检查MyBatis的依赖是否正确。项目应该包含mybatis-spring-boot-starter,否则MyBatis相关的自动配置不会生效,导致Mapper无法被识别。 然后,确认UserMapper是否在Spring的组件扫描路径下。如果应用主类所在的包与Mapper接口不在同一包或子包下,又没有使用@MapperScan指定路径,Spring可能无法发现这些接口。 还要查看配置文件,比如application.properties,是否配置了mybatis.mapper-locations,指向XML文件的位置。如果XML文件存放位置不符合默认配置,这里需要显式指定。 另外,查看是否有多个数据源配置导致冲突。如果项目中配置了多个DataSource,可能需要使用@Primary注解指定主数据源,或者在配置类中明确指定SqlSessionFactoryBean和MapperScannerConfigurer。 最后,检查IDE的编译输出目录,确保Mapper接口的.class文件已经正确生成。有时候构建工具的问题可能导致类文件未生成,需要清理并重新编译项目。 综合这些可能的原因,应该逐步引导用户检查配置、依赖和路径设置,确保所有步骤都正确无误。同时,引用相关的文档和之前的文章,帮助用户更深入地理解每个配置项的作用。</think>在Spring Boot项目中遇到`UserMapper` Bean未找到的问题,通常由组件扫描、依赖配置或注解缺失引起。以下是逐步排查和解决方案: --- ### **1. 检查Mapper接口的注解与扫描配置** - **确保Mapper接口添加了`@Mapper`注解** 每个MyBatis Mapper接口需标注`@Mapper`,否则Spring无法识别: ```java import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper { // SQL方法定义 } ``` - **或全局配置扫描路径** 在主应用类添加`@MapperScan`指定Mapper包路径[^3]: ```java @SpringBootApplication @MapperScan("com.example.mapper") // 替换为实际包名 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` --- ### **2. 验证MyBatis依赖是否正确** - **在`pom.xml`中添加MyBatis Starter依赖** 确保包含以下依赖以启用自动配置: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.3.1</version> </dependency> ``` 如果未正确引入,Spring Boot不会自动配置Mapper扫描[^1]。 --- ### **3. 检查XML映射文件路径配置** - **在`application.properties`中指定XML位置** 如果使用XML定义SQL映射,需显式配置路径: ```properties mybatis.mapper-locations=classpath:mapper/*.xml ``` 确保XML文件位于`src/main/resources/mapper/`目录下。 --- ### **4. 排除多数据源配置冲突** - **若配置了多个数据源** 需在`@Configuration`类中手动指定`SqlSessionFactory`和`MapperScannerConfigurer`,避免自动配置失效: ```java @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); return factoryBean.getObject(); } ``` --- ### **5. 验证组件扫描范围** - **确保Mapper接口在Spring Boot主类同级或子包下** 默认扫描范围是主类所在包及其子包。若Mapper接口在其他路径,需通过`@ComponentScan`扩展扫描范围[^2]。 --- ### **6. 清理并重新构建项目** - **执行Maven/Gradle清理命令** 有时IDE缓存会导致编译不更新: ```bash mvn clean install ``` 或使用IDE的`Rebuild Project`功能。 --- ### **示例项目结构** ``` src/ ├── main/ │ ├── java/ │ │ └── com.example/ │ │ ├── Application.java # 主类 │ │ └── mapper/ │ │ └── UserMapper.java # Mapper接口 │ └── resources/ │ └── mapper/ │ └── UserMapper.xml # XML映射文件 ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值