4922: 后缀子串排序
Description
对于一个字符串,将其后缀子串进行排序,例如grain
其子串有:
grain
rain
ain
in
n
然后对各子串按字典顺序排序,即:
ain,grain,in,n,rain
Input
每个案例为一行字符串。
Output
将子串排序输出
Sample Input
grain
banana
Sample Output
ain
grain
in
n
rain
a
ana
anana
banana
na
nana
HINT
两种写法(借鉴)一种string 一种char .
Code:
#include <stdio.h>
#include<iostream>
#include <string.h>
#include<string>
#include<algorithm>
using namespace std;
string xx[10005];
int a[10005];
bool cmp(int m,int n)
{
return xx[m]<xx[n];
}
int main()
{
string x;
while(cin>>x)
{
memset(a,0,sizeof(a));
for(int i=0; i<x.length(); i++)
{
xx[i]=x.substr(i,x.length()-i);
a[i]=i;
}
sort(a,a+x.length(),cmp);
for(int i=0; i<x.length();i++)
cout<<xx[a[i]]<<endl;
}
return 0;
}
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct node
{
char str[1005];
} x[1005];
bool cmp(node x,node y)
{
return strcmp(x.str,y.str)<0;
}
int main()
{
char s[1000];
while(~scanf("%s",s))
{
memset(x,0,sizeof(x));
int n=0;
for(int i=strlen(s)-1; i>=0; i--)
{
int k=0;
for(int j=i; j<strlen(s); j++)
{
x[n].str[k++]=s[j];
}
n++;
}
sort(x,x+n,cmp);
for(int i=0; i<n; i++)
{
printf("%s\n",x[i].str);
}
}
return 0;
}