[bzoj2789]Letters】的更多相关文章

考虑A中第i次出现的j字符,最终位置一定是在B中第i次出现的j字符的位置,然后即求逆序对数量,cdq/线段树即可 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define N 1000005 4 #define L (k<<1) 5 #define R (L+1) 6 #define mid (l+r>>1) 7 int n,a[N],c[N],nex[N],f[N<<2]; 8 long long…
[BZOJ2789][Poi2012]Letters Description 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. Input 第一行一个正整数n (2<=n<=1,000,000),表示字符串的长度. 第二行和第三行各一个长度为n的字符串,并且只包含大写英文字母. Output 一个非负整数,表示最少的交换次数. Sample Input 3 ABC BCA Samp…
按照顺序依次找到b串每个字母最后的位置,然后求逆序对. #include<cstdio> #define N 1000010 int n,i,j,k,g[26],nxt[N],bit[N];char a[N],b[N];long long ans; int main(){ for(scanf("%d",&n),gets(a),gets(a+1),gets(b+1),i=1;i<=n;g[a[i]]=i++)nxt[i]=g[a[i]-='A']; for(i=…
又是一道树状数组求逆序对的题目. 这一题我们可以将第二个串中的每一个字母,与第一个串中的字母做两两匹配,令第二个串第i个字母的值id[i]为该字母与第一个串中的字母匹配到的位置. 然后考虑到所求答案为最小的移动次数,那么这个最小的移动次数显然为序列id的逆序对个数. 树状数组求一求就没了. 我一看时间只剩15min了本来想看下能否打完的,没想到3min打完了.... #include<bits/stdc++.h> #define M 2000005 #define L long long #d…
题目链接 BZOJ 题解 如果我们给\(A\)中所有字母按顺序编号,给\(B\)中所有字母编上相同的号码 对于\(B\)中同一种,显然号码应该升序 然后求逆序对即可 #include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<cmath> #include<map> #define R…
处理数组A,A[i]表示字符串a的第i个字符排序后应去的位置,队列可. 对于多次出现的字符,其在a中的顺序和在b中的顺序应该是一一对应的. #include<cstdio> #include<queue> using namespace std; #define N 1000001 typedef long long ll; queue<int>qs[26]; ll ans; int n,A[N]; char a[N],b[N]; int d[N]; void Upda…
题目描述 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. 输入 第一行一个正整数n (2<=n<=1,000,000),表示字符串的长度. 第二行和第三行各一个长度为n的字符串,并且只包含大写英文字母. 输出 一个非负整数,表示最少的交换次数. 样例输入 3 ABC BCA 样例输出 2 题解 树状数组求逆序对 一个结论:将序列A通过交换相邻元素变换为序列B,需要的最小次数为A中…
Letters bzoj-2789 Poi-2012 题目大意:给定两个字符串A和B,每次交换A中相邻两个数.问至少交换多少次,可以将A变成B. 注释:$2\le n\le 10^6$ 想法:我们发现,A中任意两个相同字符的相对位置是不会发生改变的,所以我们可以直接求逆序对即可. 最后,附上丑陋的代码... ... #include<iostream> #include<cstdio> #include<vector> using namespace std; cons…
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example: Given "bcabc&q…
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example: Given "bcabc&q…
Remove Duplicate Letters I Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. Example: Given "bcabc"Return "abc" Given "cbacdcbc"Return "abcd&qu…
18.13 Given a list of millions of words, design an algorithm to create the largest possible rectangle of letters such that every row forms a word (reading left to right) and every column forms a word (reading top to bottom). The words need not be cho…
原题链接在这里:https://leetcode.com/problems/remove-duplicate-letters/ 题目: Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexic…
题目链接: C. Letters Cyclic Shift 题意: 现在一串小写的英文字符,每个字符可以变成它前边的字符即b-a,c-a,a-z这样,选一个字串变换,使得得到的字符串字典序最小; 思路: 贪心,尽量让前边的字典序变小; AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #inclu…
Given a string which contains only letters. Sort it by lower case first and upper case second. Note It's not necessary to keep the original order of lower-case letters and upper case letters. Example For "abAcD", a reasonable answer is "acb…
Description: Your task is simply to count the total number of lowercase letters in a string. Examples LowercaseCountCheck("abc") == 3 LowercaseCountCheck("abcABC123") == 3 LowercaseCountCheck("abcABC123!@€£#$%^&*()_-+=}{[]|\':…
https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical o…
2789: [Poi2012]Letters Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 278  Solved: 185[Submit][Status][Discuss] Description 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. Input   第一行一个正整数n (2<=n<=1,000,000),表示字符串的…
A. Anton and Letters time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Recently, Anton has found a set. The set consists of small English letters. Anton carefully wrote out all the letters f…
316. Remove Duplicate Letters Total Accepted: 2367 Total Submissions: 12388 Difficulty: Medium Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your resu…
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Recently, Anton has found a set. The set consists of small English letters. Anton carefully wrote out all the letters from the set in one li…
直接求逆序对就行了...时间复杂度O(nlogn) ------------------------------------------------------------------------- #include<bits/stdc++.h>   using namespace std;   #define idx(c) ((c) - 'A') #define lowbit(x) ((x) & -(x))   const int charset = 26; const int ma…
A. Letters Cyclic Shift time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output You are given a non-empty string s consisting of lowercase English letters. You have to pick exactly one non-empty su…
题目链接:Letters Removing 题意: 给你一个长度为n的字符串,给出m次操作.每次操作给出一个l,r和一个字符c,要求删除字符串l到r之间所有的c. 题解: 看样例可以看出,这题最大的难点在于每次在字符串中删除了前面的字符会对后面的字符产生影响.如何确定当前l和r所指的字符?这里由于对字符的位置查询相当于单点操作区间查询,可以用树状数组维护字符串的前缀和,这样就可以确定l和r的位置了(二分+树状数组 : 复杂度(log(n)×log(n))).再把所有的字符放到set里面进行删除操…
B. Polycarp and Letters time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s consisting only of lowercase an…
Description Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s consisting only of lowercase and uppercase Latin letters. Let A be a set of positions in the string. Let's call it pretty if following conditions are met…
Description Petya has a string of length n consisting of small and large English letters and digits. He performs m operations. Each operation is described with two integers l and r and a character c: Petya removes from the string all characters c on…
We have a string S of lowercase letters, and an integer array shifts. Call the shift of a letter, the next letter in the alphabet, (wrapping around so that 'z' becomes 'a'). For example, shift('a') = 'b', shift('t') = 'u', and shift('z') = 'a'. Now f…
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example 1: Input: "bcab…
We have a string S of lowercase letters, and an integer array shifts. Call the shift of a letter, the next letter in the alphabet, (wrapping around so that 'z' becomes 'a'). For example, shift('a') = 'b', shift('t') = 'u', and shift('z') = 'a'. Now f…