51Nod 1335 思维
2017-08-27 19:34
453 查看
题目链接
思路:
根据题意,要求最终字符串的字典序最小,则要求原字符串的小字符尽量换到前面,大的字符换到后面。
考虑将字符串从小到大排序,则与原字符一一比较后的第一个不同的字符一定是交换的起点,假设其在原串的位置为pos。
然后考虑枚举交换的终点,因为原字符串的终点字符一定是排序后新串的第pos个位置的字符。
若存在多个,则模拟翻转,与答案比较并更新。
总复杂度:O(Tn2)
代码:
思路:
根据题意,要求最终字符串的字典序最小,则要求原字符串的小字符尽量换到前面,大的字符换到后面。
考虑将字符串从小到大排序,则与原字符一一比较后的第一个不同的字符一定是交换的起点,假设其在原串的位置为pos。
然后考虑枚举交换的终点,因为原字符串的终点字符一定是排序后新串的第pos个位置的字符。
若存在多个,则模拟翻转,与答案比较并更新。
总复杂度:O(Tn2)
代码:
#include<cmath> #include<string> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; string s,ne,ans,tem; int main(){ ios::sync_with_stdio(false); int T;cin >> T; while(T--){ cin >> s;ne = s; sort(ne.begin(),ne.end()); int len = s.length(); int pos = len; for(int i=0 ;i<len ;i++){ if(s[i] > ne[i]){ pos = i; break; } } if(pos == len) cout << 0 << " " << 0 << endl; else{ ans = s; int st = pos,ed = st; for(int i=pos+1 ;i<len ;i++){ if(s[i] != ne[pos]) continue; tem = s; reverse(tem.begin()+pos,tem.begin()+i+1); if(tem < ans){ans = tem;ed = i;} } cout << st << " " << ed << endl; } } return 0; }
相关文章推荐
- 51Nod - 1413 -权势二进制 (思维题)
- 51nod 1201 整数划分(锻炼思维的好题)
- 51nod 1108 距离之和最小 V2【思维+求中位数】
- 51Nod - 1821 思维题 + 并查集 + 二分
- 51nod 1276 岛屿的数量(贪心策略__锻炼思维)
- 51nod 1596 搬货物 【思维题】
- 51Nod 1055 思维 + DP
- 51Nod - 1215 数组的宽度 思维+单调栈
- 【51Nod】1347 - 旋转字符串(思维)
- 【51Nod】1413 - 权势二进制(思维)
- 51nod 1625 贪心/思维
- 51nod 1391 01串【线段树,思维】
- 51nod 1266 蚂蚁 【思维题】
- 51NOD 1279 扔盘子(二分 + 思维)
- 【51Nod】1009 - 数字1的数量(思维,好题)
- 51nod 1396 还是01串 (思维or前缀和)
- 51nod 1069【思维】
- 51nod_1009_数字1的数量_思维好题
- 51nod 1112 1112 KGold(思维)@
- 51nod 1625 夹克爷发红包【思维+暴力+贪心】好题~