UESTC 1300 Easy Problem 水题

117 篇文章 2 订阅
53 篇文章 0 订阅

Easy Problem

Time Limit: 1000/1000MS (Java/Others)     Memory Limit: 131072/131072KB (Java/Others)
 

Given  n  strings  Ai , Each string has a non-negative cost  Ci .

Let’s define the function of string 
s
f(s)=ni=1Citot(s,i) f(s)=∑i=1nCi∗tot(s,i)

tot(s,i)       represents the number of occurrences of  s  in  Ai

Find the maximal value of function  f(s)  over all strings.

Note that  s  is not necessary to be some string from  A , and its length must be greater than zero.

Input

The first line is  n   (n105) , denoting the number of strings,containing only a to z.

Then  n  lines each line is a string  Ai .

Then a line contains  n  integers  Ci .

The sum of length of all the string will no more than  105

Ci10000

Output

Output one line representing the maximal value.

Sample input and output

Sample Input Sample Output
3
abc
abcd
cdab
1 2 3
6

Hint

Let  S  be ab, then the value will be  6 6, it’s the maximal value.


Source

The 14th UESTC Programming Contest Preliminary
在Contests里面的链接, E - Easy Problem
在Problems里面的链接, Easy Problem

My Solution

在队友帮忙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;
}

Thank you!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值