Given non-negative cost
Let’s define the function of string
Find the maximal value of function
Note that
The first line is a to z.
Then
Then a line contains
The sum of length of all the string will no more than
Output one line representing the maximal value.
| Sample Input | Sample Output |
|---|---|
3 abc abcd cdab 1 2 3 |
6 |
Let ab,
then the value will be
在队友帮忙debug的情况下,自己还是只Accepted了一个题目(┬_┬)
字母也可以是字符串 s
在读入的时候统计好每个字符串中每个字母出现的个数 str[maxn][26],然后读入权值以后forfor求出最大值,O(26n) , 26 * 10^5次不会超时
最开始的时候字符串用getchar来读取,然后判断是否换行,但linux上用”\n“判断好像不行,(毕竟不是老司机),WA了几发,然后 读入再用strlen()然后记录每个字母在每个字符串出现的次数才过了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
using namespace std;
const int maxn = 100000+8;
int str[maxn][26], c[maxn];
char ch[maxn];
int main()
{
//freopen("a.txt","r",stdin);
int n, len;
long long sum = -1,tsum = 0;
memset(str, 0, sizeof str);
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%s", ch);
len = strlen(ch);
for(int j = 0; j < len; j++)
str[i][ch[j] - 'a']++;
/* while(true){
ch=getchar();
if(isalpha(ch)==0) break;
str[i][ch - 'a']++;
}
*/
}
for(int i = 1; i <= n; i++)
scanf("%d", &c[i]);
for(int i = 0; i < 26; i++){
tsum = 0;
for(int j = 1; j <= n; j++){
tsum += str[j][i]*c[j];
}
sum = max(sum, tsum);
}
printf("%lld", sum);
return 0;
}原文:http://blog.csdn.net/prolightsfxjh/article/details/50991063