1
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
string solution(string str) {
string res = ""; if(str.empty()) return res;
unordered_map<string, int> fmp;
int pre = -1;
int cnt = 0;
for(int cur= 0; cur < str.size(); cur++){
string tmp = str.substr(pre + 1, cur - pre);
if(fmp.find(tmp) != fmp.end()){
if(cur == str.size() - 1) res += to_string(fmp[tmp]);
else continue;
}
else{
fmp.insert({tmp, ++cnt});
if(tmp.size() == 1) res.push_back('0');
else {tmp.pop_back(); res += to_string(fmp[tmp]);}
res.push_back(str[cur]);
pre = cur;
}
}
return res;
}
int main(){
cout << solution("ABBCBCABABCAABCAAB") << endl;
cout << solution("AAAAAAAAA") << endl;
}
2
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 10010;
int mx, my, n;
int a[N], b[N], c[N], d[N];
int lx[N], ly[N];
int idx;
int main(){
scanf("%d %d", &mx, &my);
scanf("%d", &n);
while(n -- ){
scanf("%d %d", &lx[idx], &ly[idx]);
idx++;
}
int mv, tx, ty; scanf("%d %d %d", &mv, &tx, &ty);
mv --;
int s = 0;
for(int i=0; i<idx; i++){
a[i] = s;
s += lx[i];
c[i] = s;
}
memset(b, 0, sizeof b);
for(int i=0; i<idx; i++) d[i] = ly[i];
int gx = tx - a[mv], gy = ty - b[mv];
for(int i=0; i<idx; i++){
a[i] += gx, b[i] += gy,
c[i] += gx, d[i] += gy;
}
int dx = 0, dy = 0;
if(a[0] < 0) dx -= a[0];
if(c[idx] > mx) dx = (mx - c[idx]);
if(b[0] < 0) dy -= b[0];
for(int i=0; i<idx; i++){
if(d[i] > my){
int ny = my - d[i];
if(ny < dy) dy = ny;
}
}
for(int i=0; i<idx; i++){
a[i] += dx, b[i] += dy;
}
for(int i=0; i<idx; i++){
printf("%d %d\n", a[i], b[i]);
}
}