前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ-2752-Seek the Name, Seek the Fame

POJ-2752-Seek the Name, Seek the Fame

作者头像
f_zyj
发布2018-01-09 10:54:28
7320
发布2018-01-09 10:54:28
举报
文章被收录于专栏:ACM小冰成长之路

ACM模版

描述

描述
描述

题解

image.png
image.png

代码

代码语言:javascript
复制
#include <iostream>
#include <cstring>
#include <vector>

using namespace std;

const int MAXN = 1e6;

int n;
char s[MAXN];
int nxt[MAXN];
vector<int> ans;

void getnxt()
{
    nxt[0] = -1;
    int i = 0, j = -1;
    int len = (int)strlen(s);
    while (i < len)
    {
        if (j == -1 || s[i] == s[j])
        {
            i++;
            j++;
            nxt[i] = j;
        }
        else
        {
            j = nxt[j];
        }
    }
}

int main()
{
    while (~scanf("%s", s))
    {
        int len = (int)strlen(s);

        getnxt();

        ans.clear();
        int pos = nxt[len - 1];
        while (pos != -1)
        {
            if (s[pos] == s[len - 1])
            {
                ans.push_back(pos + 1);
            }
            pos = nxt[pos];
        }

        for (int i = (int)ans.size() - 1; i >= 0; i--)
        {
            printf("%d ", ans[i]);
        }
        printf("%d\n", len);
    }

    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年10月09日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 描述
  • 题解
  • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档