Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
Register

## Re:建立一个替换关系，一直替换即可

Posted by yygy at 2023-05-26 22:19:47 on Problem 3389
In Reply To:建立一个替换关系，一直替换即可 Posted by:yygy at 2023-05-17 11:32:47
```// MyFirstApp.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <map>
#include <algorithm>

using namespace std;

typedef __int64 lld;

const int MAXN = 512;

//经度角a, 纬度角b
double GeodesicDistance(double r, double a1, double b1, double a2, double b2)
{
return r * acos(cos(b1) * cos(b2) * cos(a1 - a2) + sin(b1) * sin(b2));
}

struct Point
{
double x, y;

Point operator -(const Point& other) const
{
Point result;
result.x = x - other.x;
result.y = y - other.y;
return result;
}

Point operator +(const Point& other) const
{
Point result;
result.x = x + other.x;
result.y = y + other.y;
return result;
}

Point operator *(double c) const
{
Point result = *this;
result.x *= c;
result.y *= c;
return result;
}
}p[1024], q[1024];

static void Rotate90(Point& dir)
{
double x = -dir.y;
double y = dir.x;
dir.x = x;
dir.y = y;
}

int DoubleCompare(double a, double b)
{
if (fabs(a - b) < 1e-6)
{
return 0;
}

return a < b ? -1 : 1;
}
std::vector<std::string> strs;
std::map<std::string, int> ids;

std::vector<std::vector<int> > replaces;

char buff[4096];

bool GetWord(char* buff, int len, int& iter, std::string& word)
{
word.clear();

while (iter < len && !isalpha(buff[iter]) && buff[iter] != '_')
{
iter++;
}

if (iter >= len)
{
return false;
}

while (iter < len && (isalpha(buff[iter]) || buff[iter] == '_'))
{
word.push_back(buff[iter]);
iter++;
}

return true;
}

int GetSoftId(const std::string& source)
{
const std::map<std::string, int>::iterator iter = ids.find(source);
if (iter != ids.end())
{
return iter->second;
}

//新建
ids[source] = strs.size();
strs.push_back(source);
replaces.push_back(std::vector<int>());

return ids[source];
}

void Split(char* buff)
{
int len = strlen(buff);
int iter = 0;
std::string source;
GetWord(buff, len, iter, source);
int sourceId = GetSoftId(source);
std::string word;
while (GetWord(buff, len, iter, word))
{
int wid = GetSoftId(word);
replaces[sourceId].push_back(wid);
}
}

int arr[2][1000000];
int len[2];

void Replace(int& tag, int& currentLength)
{
int* input=&arr[tag][0];
int* result=&arr[tag^1][0];
len[tag^1]=0;
for (int i = 0; i < len[tag]; i++)
{
if (input[i] < 0)
{
result[len[tag^1]]=input[i];
len[tag^1]++;
continue;
}
{
{
len[tag^1]++;
}
currentLength -= strs[input[i]].size();
for (int j = 0; j < (int)add.size(); j++)
{
}
}
else
{
result[len[tag^1]]=input[i];
len[tag^1]++;
}
}

tag^=1;
}

int main()
{
int width, target, n;

while (cin >> width >> target >> n)
{
cin.getline(buff, 4096);
strs.clear();
ids.clear();
replaces.clear();

for (int i = 0; i < n; i++)
{
cin.getline(buff, 4096);
Split(buff);
}

int tag=0;
int* plan=&arr[tag][0];
len[tag]=0;

int currentLength = 0;

while (cin.getline(buff, 4096))
{

int len1 = strlen(buff);
if (len1 == 0)break;
int iter = 0;
std::string word;
while (GetWord(buff, len1, iter, word))
{
int id = GetSoftId(word);
plan[len[tag]] = id;
len[tag]++;
currentLength += strs[id].size();
}

//回车加个-1
plan[len[tag]]=-1;
len[tag]++;
}

while (currentLength < target)
{
Replace(tag, currentLength);
}

int acc = 0;
bool newline = true;
plan=arr[tag];
for (int i = 0; i < len[tag]; i++)
{
if (plan[i] < 0)
{
if (newline)
{
continue;
}
else
{
puts("");
//puts("[new line]");
acc = 0;
newline = true;
continue;
}
}
int cid = plan[i];
int space = 0;
if (!newline)
{
space++;
}
if (acc + space + (int)strs[cid].size() > width)
{
puts("");
acc = 0;
newline = true;
}

if (!newline)
{
putchar(' ');
acc++;
}

newline = false;

printf("%s", &strs[cid][0]);
acc += strs[cid].size();
}

//原来换行的要保持，等会儿加
}

return 0;
}

/*
21 103 7
vessel windless bay of horns
horns bulls spears
spears war needles
Sudden Fate catched
death_of doomed to death
Death It was the end
Sudden
death_of Fjolner
in the house of Frodi
Death
of the king
in the vessel

Fate catched
doomed to death
Fjolner
in the house of Frodi
It was the end
in the windless bay
of bulls spears
*/
```

Followed by:

User ID:
Title:

Content: