• [bzoj2789] [Poi2012]Letters


      结论是..把A串中的字符挪到B串里最近的出现这个字符的地方就能最优了。。。

      求出A串中的每个位置上的字符,之后要挪到哪个位置。然后求一波逆序对就是答案了。

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<algorithm>
     5 #define ll long long
     6 #define d double
     7 using namespace std;
     8 const int maxn=1000023;
     9 int pos[26][maxn],t[maxn];
    10 int num[26],top[26];
    11 int i,j,k,n,m;
    12 ll ans;
    13 char s1[maxn],s2[maxn];
    14  
    15 int ra,fh;char rx;
    16 inline int read(){
    17     rx=getchar(),ra=0,fh=1;
    18     while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();
    19     if(rx=='-')fh=-1,rx=getchar();
    20     while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;
    21 }
    22 inline void add(int x){while(x<=n)t[x]++,x+=x&-x;}
    23 inline int get(int x){int sm=0;while(x)sm+=t[x],x-=x&-x;return sm;}
    24  
    25 int main(){
    26     n=read(),scanf("%s%s",s1+1,s2+1);char x;int p;
    27     for(i=1;i<=n;i++)x=s2[i]-'A',pos[x][++num[x]]=i;
    28     for(i=1;i<=n;i++)
    29         x=s1[i]-'A',p=pos[x][++top[x]],
    30         ans+=i-1-get(p),add(p);
    31     printf("%lld
    ",ans);
    32 }
    View Code
  • 相关阅读:
    5.搜索-dfs、回溯、bfs
    4.排序算法
    3.二分查找
    2.双指针
    1.贪心算法
    【目录】leetcode刷题
    深度学习的优化与正则化
    什么是深度学习
    循环神经网络
    Failed to execute 'index' on 'IDBObjectStore': The specified index was not found.
  • 原文地址:https://www.cnblogs.com/czllgzmzl/p/5644903.html
Copyright © 2020-2023  润新知