【16.67%】【codeforces 667C】Reberland Linguistics
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other.
For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the “root” of the word — some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction — it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word “suffix” to describe a morpheme but not the few last characters of the string as you may used to).
Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language.
Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match.
Let’s look at the example: the word abacabaca is given. This word can be obtained in the following ways: , where the root of the word is overlined, and suffixes are marked by “corners”. Thus, the set of possible suffixes for this word is {aca, ba, ca}.
Input
The only line contains a string s (5 ≤ |s| ≤ 104) consisting of lowercase English letters.
Output
On the first line print integer k — a number of distinct possible suffixes. On the next k lines print suffixes.
Print suffixes in lexicographical (alphabetical) order.
Examples
input
abacabaca
output
3
aca
ba
ca
input
abaca
output
0
Note
The first test was analysed in the problem statement.
In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.
【题解】
这题的限制是说连续的两个串不能是一样的。
如果中间隔了一个是允许的0 0
设can[i][2]和can[i][3]分别表示从I点能否截取长度为2、长度为3的连续串;
初始化can[len-1][2] = true,can[len-2][3] = true;
转移方式如下
if (can[i+2][3] || (can[i+2][2] && s.substr(i,2)!=s.substr(i+2,2)))
{
can[i][2]=true;
·····
}
if (can[i+3][2] || (can[i+3][3] && s.substr(i,3)!=s.substr(i+3,3)))
{
can[i][3]=true;
.....
}
//每次截取到一串就加入到vector中。最后把vector用sort排下序;
//顺序输出就好;
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define LL long long
using namespace std;
const int MAXN = 1e4+10;
string s;
vector <string> a;
map <string,int> dic;
bool can[MAXN][5] = {0};
void input_LL(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
void input_int(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
cin>>s;
int len = s.size();
string temp;
for (int i = len-2;i>=5;i--)
{
if (i+1==len-1)
{
can[i][2] = true;
temp = s.substr(i,2);
if (!dic[temp])
{
dic[temp] = 1;
a.push_back(temp);
}
continue;
}
if (i+2==len-1)
{
can[i][3] = true;
temp = s.substr(i,3);
if (!dic[temp])
{
dic[temp] = 1;
a.push_back(temp);
}
continue;
}
if (can[i+2][3] || (can[i+2][2] && s.substr(i,2)!=s.substr(i+2,2)))
{
temp = s.substr(i,2);
can[i][2]=true;
if (!dic[temp])
{
dic[temp] = 1;
a.push_back(temp);
}
}
if (can[i+3][2] || (can[i+3][3] && s.substr(i,3)!=s.substr(i+3,3)))
{
temp = s.substr(i,3);
can[i][3]=true;
if (!dic[temp])
{
dic[temp] = 1;
a.push_back(temp);
}
}
}
sort(a.begin(),a.end());
len = a.size();
printf("%d\n",len);
for (int i = 0;i <= len-1;i++)
puts(a[i].c_str());
return 0;
}
【16.67%】【codeforces 667C】Reberland Linguistics的更多相关文章
- codeforces 667C C. Reberland Linguistics(dp)
题目链接: C. Reberland Linguistics time limit per test 1 second memory limit per test 256 megabytes inpu ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【24.67%】【codeforces 551C】 GukiZ hates Boxes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【16.23%】【codeforces 586C】Gennady the Dentist
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【26.67%】【codeforces 596C】Wilbur and Points
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 807D】Dynamic Problem Scoring
[题目链接]:http://codeforces.com/contest/807/problem/D [题意] 给出n个人的比赛信息; 5道题 每道题,或是没被解决->用-1表示; 或者给出解题 ...
- 【codeforces 67A】Partial Teacher
[题目链接]:http://codeforces.com/problemset/problem/67/A [题意] 给一个长度为n-1的字符串; 每个字符串是'L','R','='这3种字符中的一个; ...
- 【codeforces 821E】Okabe and El Psy Kongroo
[题目链接]:http://codeforces.com/problemset/problem/821/E [题意] 一开始位于(0,0)的位置; 然后你每次可以往右上,右,右下3走一步; (x+1, ...
- 【81.82%】【codeforces 740B】Alyona and flowers
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- Session丢失原因与解决方案
win2003 server下的IIS6默认设置下对每个运行在默认应用池中的工作者进程都会经过20多个小时后自动回收该进程, 造成保存在该进程中的session丢失. 因为Session,Appl ...
- (转)Bash 快捷键 完整版
转自:http://www.opsers.org/linux-home/base/full-version-of-bash-keyboard-shortcuts.html#toc-3 生活在 Bash ...
- 一些mysql innodb的建议
http://blog.csdn.net/yunhua_lee/article/details/8239145 原文:http://dev.mysql.com/doc/refman/5.5/en/in ...
- mysql的入门基础操作
1.数据库的简单介绍 1.1 什么是数据库,就是一个文件系统,使用标准sql对数据库进行操作 1.2 常见的数据库 oracle 是oracle公司的数据库,是一个收费的大型的数据库 DB2,是IB ...
- Linux下搭建Memcached缓存系统
首先说下抱歉,博主近期单位经常加班.博客更新有点慢.希望大家理解,草稿箱里存了不少内容,等不忙时候一点点填坑~ 在一般的站点开发学习时候.都会把数据存放在RDBMS(关系型数据库系统(Relation ...
- php实现旋转数组的最小数字
php实现旋转数组的最小数字 一.总结 1.题目描述定位法:掐准输入输出这两个关键词,然后题目意思就很清晰了 2.这个题目就是找数组的最小值 二.php实现旋转数组的最小数字 题目描述: 把一个数组最 ...
- [Webpack] Configure Prepack with Webpack
Great improvements and optimizations can be made to the output of bundled code. Prepack provides the ...
- [转载]剥析surging的架构思想
1.前言 2.通信机制 2.1 简介 在单体应用中,模块之间的调用通信通过引用加载方法或者函数来实现,但是单体应用最终都会因为团队壮大,项目模块的扩展和部署等出现难以维护的问题.随着业务需求 ...
- 【u110】灾后重建
Time Limit: 1 second Memory Limit: 128 MB [问题描述] B地区在地震过后,所有村庄都造成了一定的损毁,而这场地震却没对公路造成什么影响.但是在村庄重建好之前, ...
- 在Android实现client授权
OAuth对你的数据和服务正在变成实际上的同意訪问协议在没有分享用户password. 实际上全部的有名公司像Twitter.Google,Yahoo或者LinkedIn已经实现了它.在全部流行的程序 ...