笔试-822-cs

这篇博客探讨了三个计算机科学问题:字典序的全排列算法,给定字符串中字符最近匹配查询,以及计算括号序列的代价。全排列通过哈希表实现,查询操作使用字符串和哈希表进行,而括号序列的代价计算利用栈进行。此外,还涉及对数组进行范围查询的场景。这些问题展示了在处理字符串、数据结构和算法上的应用。
摘要由CSDN通过智能技术生成

第一题

字典序的全排列

第二题

给定一个字符串和n个操作,操作可能有两种:

  • 第一个数字为2代表查询操作,查询与对应位置的字符最近的相同字符的距离。
  • 第一个数字为1代表插入操作,在字符串尾部插入字符

如输入:

“abcda”
3
2 5
1 a
2 5

结果为

4
1

第一次查询,字符串为"abcda",距离第5个元素a的最近距离是4;
第二次查询,字符串为"abcdaa",距离第5个元素a的最近距离是1.

思路:用哈希表存储每一个字母的所在位置,每次遍历求最短距离

public static void dierti(String[] args) {
        Scanner reader=new Scanner(System.in);
        String origin=reader.nextLine();
        HashMap<Character,List<Integer>> map=new HashMap<>();
        for(int i=0;i<origin.length();i++){
            char c=origin.charAt(i);
            List<Integer> list=map.getOrDefault(c,new ArrayList<>());
            list.add(i+1);
            map.put(c,list);
        }
        StringBuffer sb=new StringBuffer(origin);
        int n=reader.nextInt();
        for(int i=0;i<n;i++){
            int process=reader.nextInt();
            if(process==2){
                int query=reader.nextInt();//查找第query位置上的元素最近的
                char c=sb.charAt(query-1);
                if(map.containsKey(c)){
                    List<Integer> list=map.get(c);
                    int minDistance=10000000;
                    System.out.println(c);
                    System.out.println(list);
                    for(int yuansu:list){
                        if(yuansu==query)
                            continue;
                        minDistance=Math.min(minDistance,Math.abs(query-yuansu));
                    }
                    if(minDistance==10000000)
                        System.out.println(-1);
                    else
                        System.out.println(minDistance);
                } else{
                    System.out.println(-1);
                }
            }else{
                String insert=reader.next();
                char c=insert.toCharArray()[0];
                sb.append(c);

                List<Integer> list=map.getOrDefault(c,new ArrayList<>());
                list.add(sb.length());
                map.put(c,list);
            }
        }
    }

第三题

空串是合法序列,代价为1。
如果s是合法序列,且代价为x;那么(s)也是合法序列,代价为x+1。
如果s和t都是合法序列,那么st也合法,代价为s×t
如(()())()是合法的,且代价为10 ,因为[(1+1)×(1+1)+1]×(1+1)=10
求给定字符串的代价,结果模1000000007

思路:用栈

    public static void disanti(String[] args) {
        Scanner reader=new Scanner(System.in);
        char[] input=reader.nextLine().toCharArray();
        Stack<Long> stk=new Stack<>();
        long res=1;
        for(int i=0;i<input.length;i++){
            if(input[i]=='('){
                stk.push(-1l);
            }else{
                if (stk.peek()==-1){//如果读到左括号就压进去一个-1
                    stk.pop();
                    stk.push(2l);
                }else {
                    long count=stk.pop();
                    if(!stk.isEmpty()){
                        while (stk.peek()>0){
                            count*=stk.pop();
                            count%=1000000007;
                        }
                    }
                    //如果还不为空,说明有一个左括号,弹出左括号-1,并给count自增
                    if(!stk.isEmpty()){
                        stk.pop();
                        count++;
                    }
                    stk.push(count);
                }
            }
        }
        while(!stk.isEmpty()){//栈内元素相乘
            res*=stk.pop();
            res%=1000000007;
        }
        System.out.println(res);
    }

第四题

对于数组做范围查询,多次调用

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值