string类 sort Educational Codeforces Round 9 C. The Smallest String Concatenation
来源:互联网 发布:知乎布鲁塞尔自由大学 编辑:程序博客网 时间:2024/06/08 03:10
题意:给1<=n<=5*10^4个长度1<=|ai|<=50的字符串,将他们拼接成一个,输出字典序最小的
1)话说此题费了一番周折,也做出来了,然后发现自己的代码比宦豪和帆神的都长好多,发现自己应该好好学学String类了,确实比char数组好用好多~~
2)此题用char数组做的时候,要对char二维数组进行快排sort,百度了一下,应该是只能用结构体
typedef struct node{ char str[55];}node;node ma[N];然后对此结构体进行排序,但要是用string类的话就不用这样了,直接开一个string 的一维数组就可以了
string str[MAXN];
3)sort排序是cmp函数是bool型,而不是int型,这个也竟然卡了一下
顺便记录一下sort的cmp函数
#include<algorithm> using namespace std;
sort(a,a+n,cmp);对a[0]到a[n-1]排序,cmp不写默认升序,从小到大
经典的greater<type>()和less<type>():
int a[10]={1,4,3,2,5,9,56,7,9,4};int main(){ int i,j; sort(a,a+10,less<int>());//从小到大,升序 sort(a,a+10,greater<int>());//从大到小,降序 for(i=0;i<10;i++) printf("%d ",a[i]); putchar('\n'); return 0;}
int型,char型,double型都可以,结构体类似:
int maint[1000];bool cmpint(int a,int b){ return a<b;//升序排列,从小到大}
二维数组a[1000][2]按照a[][0]升序排列
可悲的是sort不支持二维数组,开结构体吧
本题我的代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>#include <algorithm>#define mem(a) memset(a,0,sizeof(a))typedef long long ll;#define N 50050using namespace std;typedef struct node{ char str[55];}node;node ma[N];int n;bool comp(node a,node b){ if(a.str[0]!=b.str[0]){ return a.str[0]<b.str[0]; } else { int la=strlen(a.str),lb=strlen(b.str); int i,j,l; l=min(la,lb); for(i=0;i<l;i++){ if(a.str[i]!=b.str[i]) return a.str[i]<b.str[i]; } char a1[110],b1[110]; for(i=0;i<la;i++) a1[i]=a.str[i]; for(i=la;i<la+lb;i++) a1[i]=b.str[i-la]; a1[la+lb]='\0'; for(i=0;i<lb;i++) b1[i]=b.str[i]; for(i=lb;i<la+lb;i++) b1[i]=a.str[i-lb]; b1[la+lb]='\0'; int tmp=strcmp(a1,b1); if(tmp==0)return 0; else if(tmp>0)return 0; else return 1; }}int main(){ int i,j; while(scanf("%d",&n)){ for(i=0;i<n;i++) scanf("%s",ma[i].str); sort(ma,ma+n,comp); for(i=0;i<n;i++){ int tmp=strlen(ma[i].str); for(j=0;j<tmp;j++){ printf("%c",ma[i].str[j]); } } putchar('\n'); } return 0;}
看一下q神简洁的代码:
#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>using namespace std;const int MAXN=50005;string str[MAXN];bool cmp(string a,string b){ return a+b<b+a;}int main(){ int n; cin>>n; for(int i=0;i<n;i++) cin>>str[i]; sort(str,str+n,cmp); for(int i=0;i<n;i++) cout<<str[i]; return 0;}
0 0
- string类 sort Educational Codeforces Round 9 C. The Smallest String Concatenation
- Educational Codeforces Round 9 -- C - The Smallest String Concatenation
- Educational Codeforces Round 9 C The Smallest String Concatenation
- Educational Codeforces Round 9 C. The Smallest String Concatenation【思维】
- Educational Codeforces Round 9-C. The Smallest String Concatenation(string)
- Educational Codeforces Round 9 C - The Smallest String Concatenation ,学习到string
- Educational Codeforces Round 9 C. The Smallest String Concatenation(字符串排序)
- Educational Codeforces Round 9-C. The Smallest String Concatenation(字符串排序)
- Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串
- (Educational Codeforces Round 9 )The Smallest String Concatenation(排序)
- codeforces--623C The Smallest String Concatenation
- Codeforces 632C - The Smallest String Concatenation
- CodeForces-632C-The Smallest String Concatenation
- CodeForces 632C The Smallest String Concatenation
- CodeForces 632C The Smallest String Concatenation
- The Smallest String Concatenation CodeForces
- The Smallest String Concatenation CodeForces
- Codeforces 632C The Smallest String Concatenation 【string】
- HashMap实现原理分析
- 【实例】 python数据库编程
- Android开发应用程序发布到Google Play步骤
- Git 常用命令速查表
- JSP编译成Servlet(一)语法树的生成——语法解析
- string类 sort Educational Codeforces Round 9 C. The Smallest String Concatenation
- PAT (Advanced Level) Practise 1065 A+B and C (64bit) (20)
- 用三种方式取出给定字符串中的目标字串
- C++虚继承(二) --- C++ 对象的内存布局(上)(陈皓)
- QML中的AnchorChanges锚布局改变元素
- 搜狐设计师:我从产品经理转为交互设计师的经验总结
- Retrofit的分析以及实现步骤
- 关于Mainfest.MF文件的总结
- 命令行提取assets.car中的图片,2x and 3x图片