标签:size number ber using open include string copy onclick
4 1337 1 2 5 4 6 6 3 1 9
1557
5 11111 9 8 7 6 5 4 3 2 1
99999
2 33 1 1 1 1 1 1 1 1 1
33
给出一个字符串 和其映射 求最大字典序
#include<bits/stdc++.h> using namespace std; //input by bxd #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repp(i,a,b) for(int i=(a);i>=(b);--i) #define RI(n) scanf("%d",&(n)) #define RII(n,m) scanf("%d%d",&n,&m) #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k) #define RS(s) scanf("%s",s); #define ll long long #define pb push_back #define CLR(A,v) memset(A,v,sizeof A) ////////////////////////////////// #define inf 0x3f3f3f3f const int N=2e5+5; const int M=1000; char ma[10]; char s[N]; int main() { int n;RI(n); RS(s+1); rep(i,1,9) cin>>(ma[i+‘0‘]) ; int i=1; while(i<=n) { if(ma[s[i]]>s[i]) { while(ma[s[i]]>=s[i]&&i<=n) cout<<ma[s[i]],i++; while(i<=n) cout<<s[i],i++; break; } else cout<<s[i],i++; } return 0; }
大神简洁的写法:
#include <bits/stdc++.h> using namespace std; main() { int n; cin>>n; string a; cin>>a; char f[256]; for (int i=1,j; i<=9; ++i) cin>>j,f[‘0‘+i]=‘0‘+j; int i=0; while (i<n && a[i]>=f[a[i]]) ++i; while (i<n && a[i]<=f[a[i]]) a[i]=f[a[i]],++i; cout<<a; }
标签:size number ber using open include string copy onclick
原文地址:https://www.cnblogs.com/bxd123/p/10871892.html