Codeforces Round #246 (Div. 2)——D题
KMP算法,没写出来,完全不理解NEXT数组。现在理解了很多
答案都在程序中
,不过这个思想真的很神奇,
还有毛语不好,一直没看懂题目,现在懂了,
大概是:S中前缀等于后缀,求其长度,和其在S中出现了几次,
KMP可以直接求出各个长度,但是求次数真的真的很神奇:
/*
CJT:第一份KMP算法,呜呜
我的理解:先求出next数组,next 数组的含义是:可以跳过的
个数,也是前缀的数
先求出tot,答案数,
然后求数组中有多少个可以substring
因为nexts是关于求对称的个数,所以
可以用来后面的求个数 */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
string s;
int vis[],tot;
int ans[],next[]; void kmp()
{
int k=-,i=;
next[]=-;
while (i<s.size())
{
if (k==-||s[i]==s[k])
{
++k,++i;
next[i]=k;
}
else k=next[k];
}
} int main()
{
cin>>s;
kmp();
int l=s.size();
int id=l;
while (id)
{
++tot;
vis[id]=;
id=next[id];
}
for (int i=;i<=l;i++) ans[i]=;
for (int i=l;i>=;i--)
ans[next[i]]+=ans[i];//这里很难写
cout<<tot<<endl;
for (int i=;i<=l;i++)
if (vis[i]) cout<<i<<" "<<ans[i]<<endl;
return ;
}
Codeforces Round #246 (Div. 2)——D题的更多相关文章
- Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes
D. Prefixes and Suffixes You have a string s = s ...
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- Codeforces Round #246 (Div. 2)
题目链接:Codeforces Round #246 (Div. 2) A:直接找满足的人数,然后整除3就是答案 B:开一个vis数组记录每一个衣服的主场和客场出现次数.然后输出的时候主场数量加上反复 ...
- Codeforces Round #612 (Div. 2) 前四题题解
这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...
- Codeforces Round #713 (Div. 3)AB题
Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...
- Codeforces Round #552 (Div. 3) A题
题目网址:http://codeforces.com/contest/1154/problem/ 题目意思:就是给你四个数,这四个数是a+b,a+c,b+c,a+b+c,次序未知要反求出a,b,c,d ...
- Codeforces Round #412 Div. 2 补题 D. Dynamic Problem Scoring
D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #425 (Div. 2))——A题&&B题&&D题
A. Sasha and Sticks 题目链接:http://codeforces.com/contest/832/problem/A 题目意思:n个棍,双方每次取k个,取得多次数的人获胜,Sash ...
随机推荐
- Android开发环境下关于如何导出手机通讯录数据库【Written By KillerLegend】
首先度Linux中的权限(Permissions)进行一些说明: permissions一共有10个符号位,[- --- --- ---],在这里我们从左至右由0开始编号,各个符号位的编号分别为0,1 ...
- [.ashx檔?泛型处理例程?]基础入门#3....ADO.NET 与 将DB里面的二进制图片还原
原文出處 http://www.dotblogs.com.tw/mis2000lab/archive/2013/08/20/ashx_beginner_03_db_picture_show.aspx ...
- AngularJs记录学习02
<!doctype html> <html ng-app="myapp"> <head> <meta http-equiv="C ...
- Python核心编程--学习笔记--6--序列(下)列表、元组
11 列表 类似于C语言的数组,但是列表可以包含不同类型的任意对象.列表是可变类型. 创建列表——手动赋值.工厂函数: >>> aList = [12, 'abc'] >> ...
- Ruby Code Style
这篇博文逐渐写一点关于Ruby的编码风格,主要参考ruby-stytle-guide. 一些命名准则 函数与变量 使用蛇形小写比较好:some_var, some_function 目录和文件也使用s ...
- 刀哥多线程现操作gcd-10-delay
延迟操作 // MARK: - 延迟执行 - (void)delay { /** 从现在开始,经过多少纳秒,由"队列"调度异步执行 block 中的代码 参数 1. when 从现 ...
- Shell 内置操作符-字符串处理(汇总)
一.判断读取字符串值 表达式 含义 ${var} 变量var的值, 与$var相同 ${var-DEFAULT} 如果var没有被声明, 那么就以$DEFAULT作为其值 * ${var:-D ...
- extjs实现多国语音切换
http://kuyur.info/blog/archives/2490 http://blog.chinaunix.net/uid-28661623-id-3779637.html http://b ...
- mac下的ssh自动登陆
终端的ssh是标准的OpenSSH client 如果需要克隆会话功能,可以通过配置打开. $ cat .ssh/config Host * ControlMaster auto ControlPat ...
- TableViewCell Swipe to Delete and More Button(like mail app in iOS7 or later)
在iOS7系统的Mail App中TableViewCell的一个功能让我们做TableView的比较羡慕,就是滑动cell,右边出现了两个按钮,如下: 网上在github上有很多大牛用custom ...