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 // 大写类型 同 ...
随机推荐
- Strom运行监控
https://blog.csdn.net/wenxuechaozhe/article/details/80548709
- input标签附带提示文字(bootstrap里面输入框的两侧同时添加额外元素)
一直不太喜欢用定位,今天写界面的时候,要在输入框右边添加默认的元素符号. 第一次尝试,因为本身项目用的是bootstrap所以就想利用输入框的两侧同时添加额外元素. 但是写了代码发现效果不同. 第二次 ...
- Centos7安装完成后一些基本操作
1.基本操作一:主机名 # centos7有一个新的修改主机名的命令hostnamectl hostnamectl set-hostname --static www.node1.com # 有些命令 ...
- 深入GCD(三): Dispatch Sources
何为Dispatch Sources简单来说,dispatch source是一个监视某些类型事件的对象.当这些事件发生时,它自动将一个block放入一个dispatch queue的执行例程中.说的 ...
- ubuntu compile php from source code
10down vote Assuming that you already have the OpenSSL libraries and header files (on rpm systems th ...
- ios9定位服务的app进入后台三分钟收不到经纬度,应用被挂起问题及解决方式
原来定位服务是10分钟收不到定位信息就挂起定位,如今变为最短3分钟,预计都是为了省电吧. 仅仅要你开启应用的后台定位,而且10分钟有一次定位,那么苹果就不会关闭你的线程.如今变成3分钟.若你的应用开启 ...
- RPM安装mysql5.6
原文 http://blog.csdn.net/liumm0000/article/details/18841197 a. 检查MySQL及相关RPM包,是否安装,如果有安装,则移除(rpm –e 名 ...
- PHP将当前目录列出来
$d=dir("."); echo $d->path; while(false !== ($e = $d->read())) { echo "<a hr ...
- android-custom-tab-with-viewpager
https://github.com/eltld/android-custom-tab-with-viewpager
- HDOJ1004 数组还要自己初始化
#include <iostream> #include <stdio.h> #include "string.h"using namespace std; ...