Carryon的字符串

I: Carryon的字符串

时间限制: 1 s      内存限制: 128 MB     
我的状态

题目描述

Carryon最近喜欢上了一些奇奇怪怪的字符,字符都是英文小写字母,但奇怪的是a可能比b小,也可能比b大,好奇怪。与此同时,他拿到了好多的字符串,可是看着很不顺眼,因为他们很乱,所以他想将这些字符串按字典序从小到大排下序,这样就好看多了。由于a可能比b小,也可能比b大,这样按常规方法肯定是不行的,幸运的是他破解了26个字母的大小顺序,这样他就开开心心的将字符串从小到大排序了。

输入

第一行输入2626个字符的大小顺序

第二行输入一个n(1n105)n(1≤n≤105)。

接下来nn行,每行一个字符串sisi,数据保证每个字符串不重复。(1i=1nlen(si)3×105)(1≤∑i=1nlen(si)≤3×105)

输出

将n个字符串按字典序从小到大输出。

样例输入

abcdefghijklmnopqrstuvwxyz
5
bcda
licj
lin
aaaa
aaaaa

样例输出

aaaa
aaaaa
bcda
licj
lin

思路:这题主要讲一下string字符串的存储和在结构体里面的使用,这题也让我直到了char和string的差别,这题用char的话会报错,数组开的太大了,只能在结构体里面开string
那么思路呢? 就是用一个数组,其实就是按照题目给出的大小标准,把题目中的大小转化为a~z,把当前字符串转换成由a~z按正常大小组成的字符串,具体看代码
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=1e5+10;
const int maxk=3e5+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
char a[26];
char s[26];
struct p
{
    string x,y;//这题只能用string,如果用数组会报错
}b[maxn];
bool cmp(const p a,p b)
{
    return a.y<b.y;
}
int main()
{
    cin>>a;
    for(int i=0;i<26;i++)//这一步就是把当前字符串改为按照a~z大小的字符串
    {
        int x=a[i]-'a';
        s[x]='a'+i;
    }
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>b[i].x;
        int len=b[i].x.length();
        for(int j=0;j<len;j++)
        {
            int z=b[i].x[j]-'a';
            b[i].y+=s[z];//这里就是把原来串改为按照a~z正常大小的另外一个串
        }
    }
    sort(b,b+n,cmp);
    for(int i=0;i<n;i++)
        cout<<b[i].x<<endl;
    return 0;
}
当初的梦想实现了吗,事到如今只好放弃吗~
原文地址:https://www.cnblogs.com/caijiaming/p/9468263.html