您的位置:首页 > 其它

FOJ 1409 文件压缩

2009-08-03 17:26 204 查看
http://acm.fzu.edu.cn/problem.php?pid=1409

解题思路:这题是给你字符串,依次将第一个字母要进行左移一位,构造出若干个新单词。然后进行排序,再取出每个单词的最后一个字符,合成一个新单词,输出它,并且找到原来字符串的第一个字母在这个新字符串中的位置。输入字符串到数组中,然后拼接数组,使数组放大一倍,然后进行记录新单词。我定义了一个结构体数组对象来保存新字符串的首字母和最后一个字母,还有一个一个locate用来标记新字符串最后一个字母是原字符串的首字母。记录完所有字符串的首字母和最后一个字母后,用STL的stable_sort函数按首字母顺序排序(题目要求,首字母一样时,相对位置不可改变),最后合并尾字母即可,从中找出locate标记为1的元素位置,输出即可。

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

struct words
{
char first;
char second;
int locate;
}a[10000];

bool cmp(words a,words b)
{
return a.first<b.first;
}

int main()
{
int clen,i,locate;
bool status;
char strings[20002];
char String[10001];
while (scanf("%d%*c",&clen)!=EOF)
{
scanf("%s",strings);
status = false;
strcpy(String,strings);
strcat(strings,String);
a[0].first = strings[0];
a[0].second = strings[clen-1];
for (i=1;i<clen;i++)
{
a[i].first = strings[i];
a[i].second = strings[i+clen-1];
}
a[1].locate = 1; //标记
stable_sort(a,a+clen,cmp);
for (i=0;i<clen;i++)
{
if (!status)
{
if (a[i].locate == 1)
{
locate=i+1;
status =true;
}
}
printf("%c",a[i].second);
}
printf("/n");
printf("%d/n",locate);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: