SPOJ7758--- Growing Strings

本文介绍了一种利用AC自动机结合动态规划(DP)解决特定字符串匹配问题的方法,该问题涉及从一组照片中寻找最长的有效序列,使得序列中的每张后续照片都可能从前一张照片增长而来。

Gene and Gina have a particular kind of farm. Instead of growing animals and vegetables, as it is usually the case in regular farms, they grow strings. A string is a sequence of characters. Strings have the particularity that, as they grow, they add characters to the left and/or to the right of themselves, but they never lose characters, nor insert new characters in the middle.
Gene and Gina have a collection of photos of some strings at different times during their growth. The problem is that the collection is not annotated, so they forgot to which string each photo belongs to. They want to put together a wall to illustrate strings growing procedures, but they need your help to find an appropriate sequence of photos.
Each photo illustrates a string. The sequence of photos must be such that if si comes immediately before si+1 in the sequence, then si+1 is a string that may have grown from si (i.e., si appears as a consecutive substring of si+1). Also, they do not want to use repeated pictures, so all strings in the sequence must be different.
Given a set of strings representing all available photos, your job is to calculate the size of the largest sequence they can produce following the guidelines above.

Input

Each test case is given using several lines. The first line contains an integer N representing the number of strings in the set (1 ≤ N ≤ 10^4). Each of the following N lines contains a different non-empty string of at most 1000 lowercase letters of the English alphabet. Within each test case, the sum of the lengths of all strings is at most 10^6.

The last test case is followed by a line containing one zero.

Output

For each test case output a single line with a single integer representing the size of the largest sequence of photos that can be produced.

Sample

input
6
plant
ant
cant
decant
deca
an
2
supercalifragilisticexpialidocious
rag
0

output
4
2

Added by: ~!((@!@^&
Date: 2010-11-05
Time limit: 0.519s-11.98s
Source limit: 50000B
Memory limit: 1536MB
Cluster: Cube (Intel Pentium G860 3GHz)
Languages: All except: SCM chicken
Resource: ACM ICPC2010 – Latin American Regional

AC自动机+DP

自动机上每一个节点,这个节点的最优值来自于2部分:父亲节点(前缀), fail指针(后缀)外加以这个节点结尾的字符串的个数

/*************************************************************************
    > File Name: spoj7758.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年02月06日 星期五 13时30分51秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int MAX_NODE = 1000010; //节点个数
const int CHUILD_NUM = 26;
char str[1110000];
int dp[1110000];

struct AC_Automation
{
    int chd[MAX_NODE][CHUILD_NUM];
    int end[MAX_NODE];
    int fail[MAX_NODE];
    queue <int> qu;
    int sz;
    int ID[500];

    void Initialize()
    {
        fail[0] = 0; //root
        for (int i = 0; i < CHUILD_NUM; ++i)
        {
            ID[i + 'a'] = i;
        }
    }

    void Reset()
    {
        memset (chd, 0, sizeof(chd));
        sz = 1;
    }

    void Build_Trie(char str[])
    {
        int p = 0;
        int len = strlen (str);
        for (int i = 0; i < len; ++i)
        {
            int c = ID[str[i]];
            if (!chd[p][c])
            {
                memset (chd[sz], 0, sizeof(chd[sz]));
                end[sz] = 0;
                chd[p][c] = sz++;
            }
            p = chd[p][c];
        }
        ++end[p];
    }

    void Build_AC()
    {
        int ans = 0;
        while (!qu.empty())
        {
            qu.pop();
        }
        for (int i = 0; i < CHUILD_NUM; ++i)
        {
            if (chd[0][i])
            {
                fail[chd[0][i]] = 0;
                qu.push (chd[0][i]);
                dp[chd[0][i]] = end[chd[0][i]];
                ans = max(ans, dp[chd[0][i]]);
            }
        }
        while (!qu.empty())
        {
            int u = qu.front();
            qu.pop();
            for (int i = 0; i < CHUILD_NUM; ++i)
            {
                int &v = chd[u][i];
                if (v)
                {
                    qu.push(v);
                    fail[v] = chd[fail[u]][i];
                    dp[v] = max(dp[u], dp[fail[v]]) + end[v];
                    ans = max(ans, dp[v]);
                }
                else
                {
                    v = chd[fail[u]][i];
                }
            }
        }
        printf("%d\n", ans);
    }
}AC;

int main ()
{
    int n;
    while (~scanf("%d", &n), n)
    {
        AC.Reset();
        AC.Initialize();
        memset (dp, 0, sizeof (dp));
        for (int i = 1; i <= n; ++i)
        {
            scanf("%s", str);
            AC.Build_Trie(str);
        }
        AC.Build_AC();
    }
    return 0;
}
06-22
### 得物技术栈及开发者文档分析 得物作为一家专注于潮流商品的电商平台,其技术栈和开发者文档主要围绕电商平台的核心需求展开。以下是对得物技术栈及相关开发资源的详细解析: #### 1. 技术栈概述 得物的技术栈通常会涵盖前端、后端、移动应用开发以及大数据处理等多个领域。以下是可能涉及的主要技术栈[^3]: - **前端开发**: 前端技术栈可能包括现代框架如 React 或 Vue.js,用于构建高效、响应式的用户界面。此外,还会使用 Webpack 等工具进行模块化打包和优化。 - **后端开发**: 后端技术栈可能采用 Java Spring Boot 或 Node.js,以支持高并发和分布式架构。数据库方面,MySQL 和 Redis 是常见的选择,分别用于关系型数据存储和缓存管理。 - **移动应用开发**: 得物的移动应用开发可能基于原生技术(如 Swift/Kotlin)或跨平台框架(如 Flutter)。这有助于确保移动端应用的性能和用户体验一致性。 - **大数据云计算**: 在大数据处理方面,得物可能会使用 Hadoop 或 Spark 进行数据挖掘和分析。同时,依托云服务提供商(如阿里云或腾讯云),实现弹性扩展和资源优化。 #### 2. 开发者文档分析 类似于引用中提到的 Adobe 开发者文档模板[^2],得物也可能提供一套完整的开发者文档体系,以支持内部团队协作和外部开发者接入。以下是开发者文档可能包含的内容: - **API 文档**: 提供 RESTful API 或 GraphQL 的详细说明,帮助开发者快速集成得物的功能模块,例如商品搜索、订单管理等。 - **SDK 集成指南**: 针对不同平台(如 iOS、Android 或 Web)提供 SDK 下载和集成教程,简化第三方应用的开发流程。 - **技术博客**: 分享得物在技术实践中的经验成果,例如如何优化图片加载速度、提升应用性能等。 - **开源项目**: 得物可能将部分技术成果开源,供社区开发者学习和贡献。这不仅有助于提升品牌形象,还能吸引更多优秀人才加入。 #### 3. 示例代码 以下是一个简单的示例代码,展示如何通过 RESTful API 调用得物的商品搜索功能(假设接口已存在): ```python import requests def search_items(keyword, page=1): url = "https://api.dewu.com/v1/items/search" headers = { "Authorization": "Bearer YOUR_ACCESS_TOKEN", "Content-Type": "application/json" } params = { "keyword": keyword, "page": page, "size": 10 } response = requests.get(url, headers=headers, params=params) if response.status_code == 200: return response.json() else: return {"error": "Failed to fetch data"} # 调用示例 result = search_items("Air Jordan", page=1) print(result) ``` 此代码片段展示了如何通过 Python 请求得物的 API,并获取指定关键词的商品列表。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值