Removing Columns - CodeForces 496 C 水题

本文探讨如何通过最少次数删除列,使包含n个字符串的矩形表格按照字典序排列。通过逐列扫描比较,实现高效排序,提供算法实现代码。

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.


题意:问至少删除几列,使得n个字符串按照字典序排列。

思路:从左到右扫,每次记录当前这个字符串的前i个字符是否和上一个相同,如果相同的话,那么就后面跟的必须字典序,否则的话就可以随意了。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
char s[1010][1010];
int dp[2][1010];
int main()
{
    int T,t,n,m,i,j,k,ans=0,a,b,c;
    bool flag;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
       scanf("%s",s[i]+1);
    for(i=2;i<=n;i++)
       dp[0][i]=1;
    a=0,b=1;
    for(j=1;j<=m;j++)
    {
        i=1;
        flag=true;
        while(i<=n)
        {
            dp[b][i]=0;
            for(i++;i<=n && dp[a][i]==1;i++)
            {
                if(s[i][j]==s[i-1][j])
                  dp[b][i]=1;
                else if(s[i][j]>s[i-1][j])
                  dp[b][i]=0;
                else
                {
                    flag=false;
                    break;
                }
            }
            if(!flag)
              break;
        }
        if(flag)
          swap(a,b);
        else
          ans++;
    }
    printf("%d\n",ans);
}



root@sw:~/Serve/vscode# chmod 777 uninstall.sh root@sw:~/Serve/vscode# ./uninstall.sh Reading package lists... Done Building dependency tree... Done Reading state information... Done No apt package "code", but there is a snap with that name. Try "snap install code" E: Unable to locate package code Reading package lists... Done Building dependency tree... Done Reading state information... Done The following packages will be REMOVED: libauthen-sasl-perl* libclone-perl* libdata-dump-perl* libencode-locale-perl* libfile-basedir-perl* libfile-desktopentry-perl* libfile-listing-perl* libfile-mimeinfo-perl* libfont-afm-perl* libhtml-form-perl* libhtml-format-perl* libhtml-parser-perl* libhtml-tagset-perl* libhtml-tree-perl* libhttp-cookies-perl* libhttp-daemon-perl* libhttp-date-perl* libhttp-message-perl* libhttp-negotiate-perl* libio-html-perl* libio-socket-ssl-perl* libio-stringy-perl* libipc-system-simple-perl* liblwp-mediatypes-perl* liblwp-protocol-https-perl* libmailtools-perl* libnet-dbus-perl* libnet-http-perl* libnet-smtp-ssl-perl* libnet-ssleay-perl* libproxychains3* libtie-ixhash-perl* libtimedate-perl* libtry-tiny-perl* liburi-perl* libwww-perl* libwww-robotrules-perl* libx11-protocol-perl* libxml-parser-perl* libxml-twig-perl* libxml-xpathengine-perl* mesa-utils-bin* perl-openssl-defaults* xdg-utils* 0 upgraded, 0 newly installed, 44 to remove and 26 not upgraded. After this operation, 8,426 kB disk space will be freed. (Reading database ... 110336 files and directories currently installed.) Removing libauthen-sasl-perl (2.1600-1.1) ... Removing libclone-perl (0.45-1build3) ... Removing libdata-dump-perl (1.25-1) ... Removing libnet-dbus-perl (1.2.0-1build3) ... Removing libxml-twig-perl (1:3.52-1) ... Removing libxml-parser-perl:amd64 (2.46-3build1) ... Removing libhttp-daemon-perl (6.13-1ubuntu0.1) ... Removing libfile-mimeinfo-perl (0.31-1) ... Removing libfile-desktopentry-perl (0.22-2) ... Removing libfile-basedir-perl (0.09-1) ... Removing libhtml-format-perl (2.12-1.1) ... Removing libfont-afm-perl (1.20-3) ... Removing libhtml-form-perl (6.07-1) ... Removing libmailtools-perl (2.21-1) ... Removing libnet-smtp-ssl-perl (1.04-1) ... Removing libio-stringy-perl (2.111-3) ... Removing libipc-system-simple-perl (1.30-1) ... Removing libproxychains3:amd64 (3.1-9) ... Removing libtie-ixhash-perl (1.23-2.1) ... Removing libx11-protocol-perl (0.56-7.1) ... Removing libxml-xpathengine-perl (0.14-1) ... Removing mesa-utils-bin:amd64 (8.4.0-1ubuntu1) ... Removing xdg-utils (1.1.3-4.1ubuntu3~22.04.1) ... Removing liblwp-protocol-https-perl (6.10-1) ... Removing libwww-perl (6.61-1) ... Removing libfile-listing-perl (6.14-1) ... Removing libhtml-tree-perl (5.07-2) ... Removing libhtml-parser-perl:amd64 (3.76-1build2) ... Removing libhtml-tagset-perl (3.20-4) ... Removing libhttp-cookies-perl (6.10-1) ... Removing libhttp-negotiate-perl (6.01-1) ... Removing libio-socket-ssl-perl (2.074-2) ... Removing libnet-http-perl (6.22-1) ... Removing libnet-ssleay-perl:amd64 (1.92-1build2) ... Removing libtry-tiny-perl (0.31-1) ... Removing libwww-robotrules-perl (6.02-1) ... Removing perl-openssl-defaults:amd64 (5build2) ... Removing libhttp-message-perl (6.36-1) ... Removing libencode-locale-perl (1.05-1.1) ... Removing libhttp-date-perl (6.05-1) ... Removing libio-html-perl (1.004-2) ... Removing liblwp-mediatypes-perl (6.04-1) ... Removing libtimedate-perl (2.3300-2) ... Removing liburi-perl (5.10-1) ... Processing triggers for man-db (2.10.2-1) ... Processing triggers for libc-bin (2.35-0ubuntu3.9) ... Hit:1 http://mirrors.cloud.aliyuncs.com/ubuntu jammy InRelease Get:2 http://mirrors.cloud.aliyuncs.com/ubuntu jammy-updates InRelease [128 kB] Hit:3 http://mirrors.cloud.aliyuncs.com/ubuntu jammy-backports InRelease Get:4 http://mirrors.cloud.aliyuncs.com/ubuntu jammy-security InRelease [129 kB] Ign:5 https://download.docker.com/linux/ubuntu jammy InRelease Hit:5 https://download.docker.com/linux/ubuntu jammy InRelease Hit:6 https://packages.emqx.com/emqx/emqx/ubuntu jammy InRelease Fetched 257 kB in 3s (94.1 kB/s) Reading package lists... Done Building dependency tree... Done Reading state information... Done 26 packages can be upgraded. Run 'apt list --upgradable' to see them. Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)). 验证结果: [成功] VSCode二进制文件已移除 [警告] 存在残留进程 root@sw:~/Serve/vscode# 怎么解决?
最新发布
05-13
<think>好的,用户现在的问是,卸载VSCode后还有残留进程和依赖项,需要清理。之前我提供了一个卸载脚本,但用户可能在使用过程中遇到了残留问,所以需要进一步帮助。 首先,用户提到了站内引用中的孤儿进程处理机制,即init进程会接管并清理孤儿进程。不过,VSCode的残留进程可能不是孤儿进程,而是还在运行中的子进程,比如扩展或服务进程。这时候需要手动终止这些进程。 接下来,用户引用中提到卸载驱动时出现循环登录和黑屏的问,这可能和图形界面驱动有关,但当前问集中在VSCode的残留进程,需要区分开。不过要注意,在清理VSCode时如果涉及到图形相关的依赖,比如X11包,错误移除可能导致类似问,所以需要谨慎处理依赖项。 用户提供的卸载脚本已经处理了包卸载、配置清理和残留文件删除,但可能有些进程在脚本运行时仍在运行,导致未被正确终止。因此,需要加强进程检测和终止的部分。 另外,用户提到k8sgpt项目用AI分析Kubernetes问,这里不相关,但可能用户希望更智能的清理方法,比如自动检测所有相关进程。不过实际解决方案还是需要具体命令。 现在需要解决的步骤: 1. 确保所有VSCode相关进程被终止,包括子进程。 2. 确认并清理所有残留的依赖包,特别是可能被其他软件依赖的包。 3. 检查用户配置和系统文件是否彻底删除。 4. 处理可能因权限问导致的残留文件。 可能的解决方案: - 使用pkill或killall命令终止所有相关进程,包括子进程。 - 使用pgrep或ps命令检查进程是否存在。 - 对于依赖项,使用包管理器的查询命令找出孤立的依赖并清理。 - 检查常见残留目录,如/tmp或用户目录下的隐藏文件。 - 处理Flatpak或Snap安装的VSCode实例,这些可能需要不同的卸载方式。 需要考虑到用户可能使用不同的安装方式(如直接下载、包管理器、Snap等),所以脚本可能需要扩展来覆盖这些情况。此外,用户可能在WSL环境下,需要注意Windows进程的影响,但用户之前的脚本提到了WSL,可能需要额外检查。 最后,确保脚本在终止进程时具有足够的权限,并且避免误杀其他重要进程。可能需要更精确的进程匹配,例如使用进程名或启动路径来识别VSCode相关进程。</think>### **针对性解决方案** 基于您提供的卸载脚本和引用内容,以下是优化后的残留进程清理方案(整合进程树清理和依赖验证): --- #### **1. 强制终止残留进程** ```bash # 终止所有VSCode相关进程(包括子进程) pkill -f "code|vscode|\.vscode-server" && sleep 2 # 二次验证进程终止 if pgrep -f "code|vscode" >/dev/null; then echo "[紧急] 检测到顽固进程,强制终止..." pkill -9 -f "code|vscode" fi ``` - **技术原理** `pkill -f` 通过正则匹配进程名和参数,覆盖Electron框架生成的子进程(如扩展主机进程)[^1] `sleep 2` 确保进程终止操作完成后再验证 --- #### **2. 依赖项深度清理** ```bash # APT系统验证孤立依赖 if command -v deborphan >/dev/null; then orphan_pkgs=$(deborphan) [ -n "$orphan_pkgs" ] && apt purge -y $orphan_pkgs fi # 通用依赖检查(跨发行版) strace -e openat code 2>&1 | grep -oP '/usr/lib/[^"]+' | xargs dpkg -S | cut -d: -f1 | sort -u | xargs apt purge -y ``` - **技术细节** `strace` 跟踪二进制文件加载的库,反向定位未卸载的依赖包[^2] `deborphan` 专门检测无依赖的孤立包 --- #### **3. 增强型路径清理** ```bash # 扩展清理范围(包括临时文件) find /tmp /var/tmp -name "*vscode*" -exec rm -rf {} + locate -b ".vscode" | xargs rm -rf # 需提前运行updatedb ``` - **安全机制** ```bash # 防止误删关键路径 find / -path "*vscode*" -not -path "/home/*" -not -path "/tmp/*" -exec ls -ld {} \; 2>/dev/null ``` 先列出系统级残留再手动确认删除 --- #### **4. 多安装源覆盖** ```bash # 检查并清理非标准安装方式 [[ -x $(command -v flatpak) ]] && flatpak uninstall com.visualstudio.code [[ -x $(command -v snap) ]] && snap remove code ``` - **典型场景** 当出现`nvidia-smi`检测异常时(如引用[2]),需排查GPU驱动与GUI组件的关联: ```bash lsof -n | grep deleted | grep -E 'nvidia|vscode' # 查找被删除但仍被进程占用的文件 ``` --- ### **验证命令集** ```bash # 进程验证 pgrep -af "code|vscode" || echo "[通过] 无残留进程" # 文件验证 updatedb && locate -b "code" | grep -vE '/home/|/tmp/' | while read f; do [ ! -e "$f" ] || echo "[残留] $f"; done # 依赖验证 apt check 2>&1 | grep -v "may be broken" # 检查包管理器完整性 ``` --- ### **关键知识点** 1. **孤儿进程处理** 如引用[1]所述,真正的孤儿进程会被init接管自动回收。但VSCode残留的多为**僵尸进程**或**守护进程**,需手动清理: ```bash ps -elf | awk '$5 == 1 && $15 ~ /code/{print $4}' | xargs kill # 杀死被init接管的残留进程 ``` 2. **驱动级清理** 若遇到类似引用[2]的显示问,需检查OpenGL/Vulkan库关联: ```bash ldd /usr/bin/code | grep -i nvidia # 验证二进制文件与GPU驱动的关联 ``` [^1]: 对Electron应用的多进程架构,需采用模式匹配终止所有相关进程 [^2]: 通过系统调用追踪可以精确发现隐藏依赖,此方法也适用于诊断其他软件的依赖问
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值