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;
    }
  • 相关阅读:
    【搜索】棋盘 luogu-3956
    【动态规划】石子合并 luogu-1880
    【动态规划】合唱队形 luogu-
    【模拟】报名签到 luogu-4445
    【排序+贪心】导弹拦截 luogu-1158
    【模拟】不高兴的津津
    【模拟】选数 luogu-1037
    「JOISC2020」建筑装饰 4
    「清华集训」小 Y 和恐怖的奴隶主
    「CF708E」Student's Camp
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9064324.html
Copyright © 2011-2022 走看看