本文主要是介绍CF #283 (Div. 2) B.(字符串好多坑),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接:http://codeforces.com/contest/496/problem/B
解题思路:
首先明确可以暴力,写一个add函数和shift函数。然后给一个循环值cnt,做cnt次循环即可,每次取两种情况的最小值入字符串数组,最后排下序,输出最小的字符串。
这个cnt很不好控制,我也是WA了几次才估计出来的·······
接下来说说奇葩的数据:
Input: 1
1
Output:0
Input: 4
4444
Output:0000
以上两种情况作特殊处理。
完整代码:
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;const int maxn = 100004;
int n;string Add(string s)
{for(int i = 0 ; i < n ; i ++){int t = s[i] - '0';t += 1;if(t > 9)t = 0;s[i] = (char)(t + 48);}return s;
}string shift(string s)
{char ch = s[n-1];for(int i = n - 2 ; i >= 0 ; i --){s[i+1] = s[i];}s[0] = ch;return s;
}bool cmp(string a , string b)
{return a < b;
}string ans[100004];int main()
{#ifdef DoubleQfreopen("in.txt","r",stdin);#endifstd::ios::sync_with_stdio(false);std::cin.tie(0);while(cin >> n){string s;cin >> s;if(n == 1){cout << "0" << endl;continue;}int flag = 0;for(int i = 1 ; i < n ; i ++)if(s[i] != s[i-1]){flag = 1;break;}if(flag == 0){for(int i = 0 ; i < n ; i ++)cout << "0";cout << endl;continue;}int cnt = 100001;int i = 0;ans[i++] = s;while(cnt--){string k1 = Add(s);string k2 = shift(s);if(k1 > k2)s = k2;elses = k1;ans[i++] = s;}sort(ans , ans + i , cmp);cout << ans[0] << endl;s = "";for(int i = 0 ; i < maxn ; i ++)ans[i] = "";}
}
这篇关于CF #283 (Div. 2) B.(字符串好多坑)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!