POJ 3852 StringLD 左字符串模拟

本文介绍了一个字符串处理问题,要求在给定的单词列表中,连续从每个单词左侧删除字符直至出现空字符串或重复字符串,旨在寻找最多能进行几步操作。通过逆序存储字符串并记录长度信息来提高效率。

原题地址http://poj.org/problem?id=3852


String LD
Time Limit:1000MS Memory Limit:65536K

Description

Stringld (left delete) is a function that gets a string and deletes its leftmost character (for instance Stringld(“acm”) returns “cm”).

You are given a list of distinct words, and at each step, we apply stringld on every word in the list. Write a program that determines the number of steps that can be applied until at least one of the conditions become true:

1) A word becomes empty string, or
2) a duplicate word is generated.

For example, having the list of words aab, abac, and caac, applying the function on the input for the first time results in ab, bac, and aac. For the second time, we get b, ac, and ac. Since in the second step, we have two ac strings, the condition 2 is true, and the output of your program should be 1. Note that we do not count the last step that has resulted in duplicate string. More examples are found in the sample input and output section.

Input

There are multiple test cases in the input. The first line of each test case is n (1 <= n <= 100), the number of words.
Each of the next n lines contains a string of at most 100 lower case characters.
The input terminates with a line containing 0.

Output

For each test case, write a single line containing the maximum number of stringld we can call.

Sample Input

4
aaba
aaca
baabcd
dcba
3
aaa
bbbb
ccccc
0

Sample Output
1
2
方法点击,由于对字符串从左边删除字符时间复杂度比较大,虽然本题的数据规模不是很大,可以在存储的时候将字符串逆置,然后再进行字符串操作,这里为了减少时间复杂度,额外使用了数组来保存记录字符串的长度信息。
代码如下:
/**************************************************************************
* Problem: POJ 3852 StringLD
* Copyright 2011 by Yan
* DATE:
* E-Mail: yming0221@gmail.com
************************************************************************/
#include <stdio.h>

int n;
char value[101][101];
int len[101];/*保存字符串的长度*/
char cache[101];

int main()
{
    int i,j,k,cnt;
    /*freopen("input","r",stdin);*/
    while(scanf("%d",&n)&&n!=0)
    {
    	cnt=0;
    	for(i=0;i<n;i++) len[i]=0;
        for(i=0;i<n;i++)
        {
            scanf("%s",cache);
            len[i]=strlen(cache);
            for(j=0;j<len[i];j++)/*实现字符串的逆序存储*/
			{
				value[i][j]=cache[len[i]-j-1];
			}
			value[i][len[i]]='\0';
        }
        while(1)
        {
        	cnt++;
			for(i=0;i<n;i++)/*每个字符串删除一个字符*/
			{

				len[i]--;
				value[i][len[i]]='\0';
			}
			for(i=0;i<n;i++)
			{
				if(len[i]==0) goto next;/*有字符串为空串了*/

				for(k=0;k<n;k++)
					for(j=k+1;j<n;j++)
						if(len[k]==len[j])/*减少不必要的字符串比较*/
						{
							/*printf("%s   %s\n",value[k],value[j]);*/
							if(strcmp(value[k],value[j])==0) goto next;
						}
			}
        }
next:

		printf("%d\n",--cnt);
    }
    return 0;
}

AI 代码审查Review工具 是一个旨在自动化代码审查流程的工具。它通过集成版本控制系统(如 GitHub 和 GitLab)的 Webhook,利用大型语言模型(LLM)对代码变更进行分析,并将审查意见反馈到相应的 Pull Request 或 Merge Request 中。此外,它还支持将审查结果通知到企业微信等通讯工具。 一个基于 LLM 的自动化代码审查助手。通过 GitHub/GitLab Webhook 监听 PR/MR 变更,调用 AI 分析代码,并将审查意见自动评论到 PR/MR,同时支持多种通知渠道。 主要功能 多平台支持: 集成 GitHub 和 GitLab Webhook,监听 Pull Request / Merge Request 事件。 智能审查模式: 详细审查 (/github_webhook, /gitlab_webhook): AI 对每个变更文件进行分析,旨在找出具体问题。审查意见会以结构化的形式(例如,定位到特定代码行、问题分类、严重程度、分析和建议)逐条评论到 PR/MR。AI 模型会输出 JSON 格式的分析结果,系统再将其转换为多条独立的评论。 通用审查 (/github_webhook_general, /gitlab_webhook_general): AI 对每个变更文件进行整体性分析,并为每个文件生成一个 Markdown 格式的总结性评论。 自动化流程: 自动将 AI 审查意见(详细模式下为多条,通用模式下为每个文件一条)发布到 PR/MR。 在所有文件审查完毕后,自动在 PR/MR 中发布一条总结性评论。 即便 AI 未发现任何值得报告的问题,也会发布相应的友好提示和总结评论。 异步处理审查任务,快速响应 Webhook。 通过 Redis 防止对同一 Commit 的重复审查。 灵活配置: 通过环境变量设置基
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值