Codeforces Round #283 (Div. 2)——C. Removing Columns

本文介绍了一种通过移除特定列使矩形表格按字典序排列的方法,并提供了一个示例性的C语言程序实现,该程序能有效地计算出使表格行按字母顺序排列所需的最小操作次数。
C. Removing Columns
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table

abcd
edfg
hijk

 

we obtain the table:

acd
efg
hjk

 

A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.

Input

The first line contains two integers  — n and m (1 ≤ n, m ≤ 100).

Next n lines contain m small English letters each — the characters of the table.

Output

Print a single number — the minimum number of columns that you need to remove in order to make the table good.

Sample test(s)
Input
1 10
codeforces
Output
0
Input
4 4
case
care
test
code
Output
2
Input
5 4
code
forc
esco
defo
rces
Output
4
Note

In the first sample the table is already good.

In the second sample you may remove the first and third column.

In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).

Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t.


这种题用bfs肯定要爆内存,所以没办法爆搜

仔细想想,如果遇到不合法的,一定要去掉,用一个数组标记第j列是否要去掉,然后比较字典序就行了


#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

bool vis[110];
char str[110][110];

int main()
{
	int n, m;
	while (~scanf("%d%d", &n, &m))
	{
		for (int i = 0; i < n; ++i)
		{
			scanf("%s", str[i]);
		}
		int ans = 0;
		memset (vis, 0, sizeof(vis));
		while (1)
		{
			bool flag = false;
			for (int i = 1; i < n; ++i)
			{
				for (int j = 0; j < m; ++j)
				{
					if (vis[j] || str[i][j] == str[i - 1][j])
					{
						continue;
					}
					if (str[i][j] > str[i - 1][j])
					{
						break;
					}
					if (str[i][j] < str[i - 1][j])
					{
						flag = true;
						vis[j] = 1;
						ans++;
					}
				}
			}
			if (!flag)
			{
				break;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}



<think>好的,我现在要解决用户的问题,关于在QT5 CMake项目中加载资源文件的教程。用户提到他们在子项目的test项目中遇到了资源文件未被编译进可执行文件的问题,所以需要正确的配置方法。 首先,我需要回忆一下QT资源系统的基本知识。QT使用.qrc文件来管理资源,这些资源在编译时会被处理并嵌入到可执行文件中。在CMake项目中,需要使用Qt提供的函数来处理这些资源文件,确保它们被正确编译和链接。 用户提供的引用[1]提到,他们在CMakeLists.txt中没有将资源文件包含进去,导致图片无法加载。这说明关键步骤可能在于正确使用CMake命令来添加资源。根据引用[2],手动加载资源需要创建resources.qrc文件,并在CMake中配置。 接下来,我需要确定正确的CMake配置步骤。Qt的CMake集成通常需要find_package来引入必要的模块,比如Core和Widgets。然后,使用qt_add_resources函数来添加.qrc文件,这样CMake会处理资源文件并将其链接到目标可执行文件。 可能的步骤如下: 1. 确保项目结构正确,资源文件存放在指定目录,比如resources/。 2. 创建resources.qrc文件,列出需要包含的资源,如图片、qss文件。 3.CMakeLists.txt中,使用find_package引入Qt5组件。 4. 使用add_executable定义可执行文件5. 使用qt_add_resources将资源文件添加到目标。 6. 确保目标链接到Qt5库。 需要注意的是,CMake的版本和Qt的版本可能有不同的语法要求。例如,较新的Qt版本推荐使用qt_add_resources而不是直接添加资源文件到可执行文件。此外,资源文件路径需要正确设置,确保CMake能找到.qrc文件。 用户可能在子项目中遗漏了qt_add_resources步骤,导致资源未被编译。需要检查子项目的CMakeLists.txt,确认是否调用了该函数,并且资源文件路径是否正确。 另外,引用[2]提到使用手动加载资源,可能需要通过Q_INIT_RESOURCE来初始化资源,尤其是在静态库的情况下。这部分也需要在代码中处理,但用户的问题主要集中在CMake配置,所以可能重点放在构建系统的配置上。 总结步骤: 1. 创建资源目录和.qrc文件。 2.CMakeLists.txt中配置qt_add_resources。 3. 确保可执行文件链接到处理后的资源。 需要验证这些步骤是否正确,并检查常见的错误点,比如路径错误、遗漏的Qt组件、或者未正确链接资源到目标。</think>### QT5 CMake项目加载资源文件完整教程 在Qt5项目中使用CMake管理资源文件需要三个核心步骤: 一、创建资源目录结构 ``` project_root/ ├── CMakeLists.txt ├── src/ └── resources/ ├── images/ │ └── icon.png └── resources.qrc # 资源索引文件 ``` 二、配置resources.qrc文件 ```xml <RCC> <qresource prefix="/"> <file>images/icon.png</file> </qresource> </RCC> ``` 三、CMakeLists.txt关键配置 ```cmake cmake_minimum_required(VERSION 3.16) project(MyQtProject) # 查找Qt5组件 find_package(Qt5 COMPONENTS Core Widgets REQUIRED) # 添加可执行文件 add_executable(${PROJECT_NAME} src/main.cpp) # 添加资源文件(关键步骤) qt_add_resources(${PROJECT_NAME} "app_resources" PREFIX "/" FILES resources/resources.qrc ) # 链接Qt库 target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Widgets) ``` 代码中加载资源示例: ```cpp // 使用资源路径(自动映射到内存) QIcon appIcon(":/images/icon.png"); ``` 解决方法]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值