SPOJ:String Play (?)
String Play
Milo has a string S of length L. Tutu picks a random prefix and Mota picks a random suffix ofS.
Now, Chotku is given a task of concatenating the two strings that Tutu and Mota have chosen, in respective order. Chotku wonders, how many distinct prefix-suffix concatenation is possible out there of string S.
So you know what to do.. Help Chotku!
Input
Input file contains several lines of text. Each line contains a string, S. End of file marks the end of input.
Output
Output one integer per string, denoting the number of distinct prefix-suffix concatenation of the string.
Constraints
Strings consist of lower-case letters only.
1 ≤ L ≤ 10000006
|
Sample Input |
Output for Sample Input |
|
abc |
8 |
Explanation:
For sample #1, the 3 prefixes are “a”, “ab”, “abc”
The 3 suffixes are “c”, “bc”, “abc”
And the 8 distinct concatenations are, “ac”, “abc”, “aabc”, “abbc”, “ababc”, “abcc”, “abcbc”, “abcabc”.
题意:一个字符串,A取一段前缀,B取一段后缀,然后连接起来组成一个新的字符串S,问可以组成多少种新的字符串,重复的只统计一次。
思路:首先必须是O(n)的复杂度,然后我什么都不知道了。
#include<bits/stdc++.h>
const int maxn=;
using namespace std;
char c[maxn]; int num[];
int main()
{
int Len,i; long long ans;
while(~scanf("%s",c+)){
Len=strlen(c+); ans=Len;
memset(num,,sizeof(num));
for(i=;i<=Len;i++) num[c[i]-'a']++;
for(i=Len-;i>=;i--){
ans+=Len-num[c[i+]-'a'];
if(c[i+]==c[Len]) ans++;
}
printf("%lld\n",ans);
}
return ;
}
SPOJ:String Play (?)的更多相关文章
- 【Java】Java创建String时,什么情况放进String Pool?
对Java创建String是否放入String pool作代码性的试验. 参考的优秀文章 JAVA面试题解惑系列(二)——到底创建了几个String对象? public String(String o ...
- Date类为什么设计为可变的,而不是像String一样?
首先,不得不承认,这确实是类库设计的一个错误,所以"为什么"进行了这个错误设计并没有意义.但没有事物一诞生就是完美的,我们的Java只是反应的慢了一点,再慢了一点. 更何况,Dat ...
- 你真的了解String吗?(修正版)
修正前:new出来的对象,会在堆中存放真正的值: 大错特错!!!! 修正后:new出来的对象,堆存放的并不是真正的值,而是常量池中字符串常量的地址. 一.抛砖引玉 不知道大家在做面试题时是否会遇到 ...
- 众所周知,static修饰的成员只实例化一次,而string类型每次赋值都会重新创建一个实例,那么用static修饰string呢?
string 类型每次实例化都会重新创建一个实例: 解释:string 类型重载了运算符 “=” ,每次 “=” 操作都是一次 “new”. static 修饰符的成员只实例化一次?? 解释:这个说法 ...
- .NET中的字符串(2):你真的了解.NET中的String吗?
概述 String在任何语言中,都有它的特殊性,在.NET中也是如此.它属于基本数据类型,也是基本数据类型中唯一的引用类型.字符串可以声明为常量,但是它却放在了堆中.希望通过本文能够使大家对.NET中 ...
- StringBuilder 比 String 快?空嘴白牙的,证据呢!
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 聊的是八股的文,干的是搬砖的活! 面我的题开发都用不到,你为什么要问?可能这是大部分 ...
- 你,确定了解Java的String字符串?
本文将描述JDK6中String.intern()是如何实现的,以及在JDK7和JDK8中对字符串池化技术做了哪些改变. String池化介绍 String池化就是把一些值相同,但是标识符不同的字符串 ...
- 在aspx怎么引用public string getPicurl(string picurl)?
刚才在论坛上看到一帖: Insus.NET尝试做了一下,直接使用一个Img标签是无法实现.因为函数中返回的即是一个img html标签,因此在aspx页再不能使用Img了. 现在可以回到网友的问题,那 ...
- JavaScript中如何检测一个变量是一个String类型?
typeof x === "string" typeof(x) === "string' // 小写 x.constructor === String // 大写类型 同 ...
随机推荐
- ORACLE RMAN增量备份经典理解
http://blog.itpub.net/26118480/viewspace-1793548/
- alibaba/fastjson 之 JSONPath
JOSNPath 是一个非常强大的工具,对于处理 json 对象非常方便. 官方地址:https://github.com/alibaba/fastjson/wiki/JSONPath 基本用法:ht ...
- 洛谷P1352 没有上司的舞会
题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri, ...
- 7.Java web—tomcat9部署
1)安装 在此之前要安装 好jdk和jre 下载绿色版 http://tomcat.apache.org/ 解压至:D:\Program Files (x86)\tomcat9 环境变更path添加两 ...
- 【Nutch基础教程之七】Nutch的2种执行模式:local及deploy
在对nutch源码执行ant runtime后,会创建一个runtime的文件夹.在runtime文件夹下有deploy和local 2个文件夹. [jediael@jediael runtime]$ ...
- [LeetCode][Java] Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. 题意: 写出一个函 ...
- Unicode解码转换为中文
Unicode转中文2:Regex.Unescape(string str);str格式:"\uxxxx" ,举例:"\u300d"
- webform的操作完之后返回主页面的行定位
1.在repeater表格的行绑定时给行一个id(唯一id),此地方为绑定该表格的主键. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDA3OD ...
- 关于 underscore 中模板引擎的应用演示样例
//关于 underscore 中模板引擎的应用演示样例 <!doctype html> <html> <head> <meta charset=" ...
- ArcGIS教程:分水岭
摘要 确定栅格中一组像元之上的汇流区域. 使用方法 · 各个分水岭的值将取自输入栅格中源的值或者要素倾泻点数据.假设倾泻点为栅格数据集,则使用像元值.假设倾泻点为点要素数据集,则从指定的字段中获取值. ...