Codeforces Round #294 (Div. 2)

D. A and B and Interesting Substrings
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A and B are preparing themselves for programming contests.

After several years of doing sports programming and solving many problems that require calculating all sorts of abstract objects, A and B also developed rather peculiar tastes.

A likes lowercase letters of the Latin alphabet. He has assigned to each letter a number that shows how much he likes that letter (he has assigned negative numbers to the letters he dislikes).

B likes substrings. He especially likes the ones that start and end with the same letter (their length must exceed one).

Also, A and B have a string s. Now they are trying to find out how many substrings t of a string s are interesting to B (that is, t starts and ends with the same letter and its length is larger than one), and also the sum of values of all letters (assigned by A), except for the first and the last one is equal to zero.

Naturally, A and B have quickly found the number of substrings t that are interesting to them. Can you do it?

Input
The first line contains 26 integers xa, xb, …, xz ( - 105 ≤ xi ≤ 105) — the value assigned to letters a, b, c, …, z respectively.

The second line contains string s of length between 1 and 105 characters, consisting of Lating lowercase letters— the string for which you need to calculate the answer.

Output
Print the answer to the problem.

Sample test(s)
input
1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1
xabcab
output
2
input
1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1
aaa
output
2
Note
In the first sample test strings satisfying the condition above are abca and bcab.

In the second sample test strings satisfying the condition above are two occurences of aa.
對字符串從頭至尾進行累加和,s[i]爲字符串i位置的值,滿足條件的子串{Ai……..Aj},有s[i]==s[j-1],因此對於每個字母進行從前至後比較就行,具體見代碼 。

#define LL __int64
#define N 100005
const int M=26;
vector <int>g[N];
int c[M];
LL s[N];
char str[N];
int main()
{
    int i,j,t,n;
    for(i=0;i<M;++i){
        scanf("%d",&c[i]);
    }
    scanf("%s",str);
    s[0]=0;
    for(i=0;str[i]!='\0';++i){
        t=str[i]-'a';
        s[i+1]=s[i]+c[t];
        g[t].push_back(i+1);
    }
    LL ans=0;
    for(i=0;i<M;++i){
        map<LL,int>Map;
        for(j=0;j<g[i].size();++j){
            int p=g[i][j];
            ans+=Map[s[p-1]];
            Map[s[p]]++;
        }
        Map.clear();
    }
    cout<<ans<<endl;
    return 0;
}

更精簡的:

#define LL __int64
#define N 100005
const int M=26;
int c[M];
map<LL,int>p[M]; //26個字母對應26個不同的map容器
string str;
int main()
{
    int i;
    for(i=0;i<M;++i){
        scanf("%d",&c[i]);
    }
    cin>>str;
    LL ans=0,tmp=0;
    for(i=0;str[i]!='\0';++i){
        int t=str[i]-'a';
        ans+=p[t][tmp];
        tmp+=c[t];
        ++p[t][tmp];
    }
    cout<<ans<<endl;
    return 0;
}
發佈了336 篇原創文章 · 獲贊 29 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章