zoukankan      html  css  js  c++  java
  • 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

    1. Strings consist of lower-case letters only. 

    2. 1 ≤ L ≤ 10000006

    Sample Input

    Output for Sample Input

    abc
    aab

    8
    7

    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=10000010;
    using namespace std;
    char c[maxn];  int num[27];
    int main()
    {
        int Len,i; long long ans;
        while(~scanf("%s",c+1)){
            Len=strlen(c+1); ans=Len;
            memset(num,0,sizeof(num));
            for(i=1;i<=Len;i++) num[c[i]-'a']++; 
            for(i=Len-1;i>=1;i--){
                ans+=Len-num[c[i+1]-'a'];
                if(c[i+1]==c[Len]) ans++;
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    转让malloc()该功能后,发生了什么事内核?附malloc()和free()实现源
    使用智能移动设备访问Ossim制
    POJ 3207 Ikki&#39;s Story IV
    AndroidMainifest标签说明2——&lt;activity&gt;
    POJ1149 PIGS 【最大流量】
    POJ3617 Best Cow Line 馋
    颜色(color)转换为三刺激值(r/g/b)(干股)
    关于SQL中的Update语句
    Java Script 正则表达式的使用示例
    Javascript 知识点简介
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9064324.html
Copyright © 2011-2022 走看看