计算最大乘积

给定一个元素类型为小写字符串的数组,请计算两个没有相同字符的元素 长度乘积的最大值,如果没有符合条件的两个元素,返回0。

// 本题为考试多行输入输出规范示例,无需提交,不计分。
var readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal:false
});
rl.on('line', function(line){ // javascript每行数据的回调接口
      let arr = line.split(',');
      let result = 0;
      const hasSameNumber = function(str1,str2){
          let map = new Map();
          for(let char of str1){
              map.set(char,true)
          }
          for(let char of str2){
              if(map.has(char)){
                  return false;
              }
          }  
          return true
      }
      for(let i = 0;i<arr.length;i++){
          for(let j = i+1;j<arr.length;j++){
              if(hasSameNumber(arr[i],arr[j])){
                  result = result>arr[i].length*arr[j].length?result:arr[i].length*arr[j].length;
              }
          }
      }
    console.log(result)
});

while True:
    try:
        ls =input().split(",")
        n,res = len(ls),0
        for i in range(n):
            for j in range(i+1,n):
                if len(set(ls[i]) & set(ls[j])) <= 0:
                    temp = len(ls[i]) * len(ls[j])
                    res = max(res,temp)
        print(res)
    except:
        break //manfen

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll p = 998244353;

vector<string>res;
int main() {
    string s;
    cin >> s;
    string now;
    for(int i = 0; i < s.size(); i++) {
        if(s[i] == ',') {
            res.push_back(now);
            now = "";
        } else now += s[i];
    }
    res.push_back(now);
    int ans = 0;
    for(int i = 0; i < (int)res.size(); i++) {
        for(int j = 0; j < i; j++) {
            int now = (int)res[i].size() * (int)res[j].size();
            for(auto q1 : res[i]) {
                for(auto q2 : res[j]) {
                    if(q1 == q2) now = 0;
                }
            }
            ans = max(ans, now);
        }
    }
    cout << ans << endl;
}

import java.util.Scanner;
public class Main {
    public static int max;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String str = in.nextLine();
            String[] strS = str.split(",");
            max = 0;
            for(int i=0;i<strS.length;i++){
                for(int j=i+1;j<strS.length;j++){
                    compute(strS[i],strS[j]);
                }
            }
            System.out.println(max);
        }
    }
    public static void compute(String a,String b){
        int mul = a.length()*b.length();
        if(mul<=max){
            return;
        }else{
            for(int i=0;i<a.length();i++){
                for(int j=0;j<b.length();j++){
                    if(a.charAt(i)==b.charAt(j)){
                        return;
                    }
                }
            }
            max = mul;
        }
    }
}


全部评论

相关推荐

12-15 12:50
河北工程大学
sta666:我也是这个国际商业化的,三天,一天一面,就通过了,不过我是后端实习生,好好面感觉能过。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务