Codeforces 1144 E. Median String
2019-04-02 20:59
253 查看
原题链接:https://codeforces.com/problemset/problem/1144/E
tag:字符串模拟,大整数。
题意:给定两个字符串,求字典序中间串。
思路:可以把这个题当做一个26进制数的模拟。a~z分别代表0~25,然后求平均,在通过数字反求字符串。
然后写出这么个代码
#include<bits/stdc++.h> using namespace std; typedef unsigned long long ll; ll _input(ll); void _output(ll,ll); int main() { ll n; cin>>n; ll num1,num2; num1=_input(n); num2=_input(n); ll res=0; res=(num1+num2)/2; _output(res,n); cout<<endl; return 0; } ll _input(ll n) { ll re=0; char a; for(ll i=0;i<n;i++) { cin>>a; re=re*26+(a-'a'); } return re; } void _output(ll res,ll n) { stack<char> qq; while(n--) { qq.push('a'+res%26); res/=26; } while(qq.size()) { cout<<qq.top(); qq.pop(); } }View Code
然后你就可以很快的wa了。
???
好吧,想着总不能爆掉我的unsigh long long 吧,然后打脸了。 26^10000,,,,随便爆。。
于是乎只能用数组模拟,一来可以方便的表示26进制,二来也可以表示很大的数据,爆不掉(除非非常非常变态了)
代码如下
#include<bits/stdc++.h> using namespace std; int a[1000005]; int main() { int n; cin>>n; string c,b;//储存字符串 cin>>c; cin>>b; for(int i=n-1;i>=0;i--) //模拟加法 { a[i+1]+=c[i]-'a'+b[i]-'a'; //从a[1]开始存,a[0]留着进位用。 a[i]+=a[i+1]/26; //给高一位先进位 a[i+1]%=26; //当前位对26取模。 } char e;
//模拟除法 e='a'; if(a[0]>=2)cout<<char(e+a[0]/2); if(a[0]%2!=0)a[1]+=26; //看最高位是否存在进位的,如果存在,退回来。 for(int i=1;i<=n;i++) { e='a'; cout<<char(e+a[i]/2); //输出当前位除2所表示的数。 if(a[i]%2!=0)a[i+1]+=26; //剩下的退回下一位。 } cout<<endl; return 0; }
相关文章推荐
- CodeForces 447-B. DZY Loves String
- codeforces 676C C. Vasya and String(二分)
- 【动态规划】【最短路】Codeforces 710E Generate a String
- Codeforces 676 C. Vasya and String
- 【CodeForces 624C】Graph and String
- Codeforces 710 E. Generate a String (dp)
- CodeForces 733A - Grasshopper And the String
- [Codeforces 946F]Fibonacci String Subsequences
- 【CodeForces】889 B. Restoration of string
- CodeForces 166C Median
- 【CodeForces】501B - Misha and Changing Handles(STL - string & vector & pair)
- codeforces - Queries on a String(模拟+取余)
- 【CodeForces】914 F. Substrings in a String bitset
- CodeForces - 632C The Smallest String Concatenation(string乱搞)
- CodeForces 624B - Making a String
- Codeforces 889B (Codeforces Round #445 Div. 1) Restoration of string 图论
- codeforces 889B. Restoration of string(拓扑排序)
- codeforces 799 D String Game (二分答案)
- Codeforces 196 D. The Next Good String
- Codeforces 877 B Nikita and string