• 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;
    }
  • 相关阅读:
    线段树区间异或--差分时间复杂度优化
    数独
    sql语句-根据动态参数去拼sql
    Python-读取文件的大小
    Docker-教你如何通过 Docker 快速搭建各种测试环境
    Docker-本地镜像发布到阿里云
    Docker- Mysql数据库主从同步配置方法
    mysql-如何删除主从同步
    Docker数据卷的介绍和使用
    Docker镜像-拉取并且运行
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9064324.html
Copyright © 2020-2023  润新知