-
Notifications
You must be signed in to change notification settings - Fork 0
/
strUtil.cpp
288 lines (262 loc) · 8.15 KB
/
strUtil.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//
// Created by Lenovo on 2020/10/22.
//
// codeThinking.cpp : 定义控制台应用程序的入口点。
//
//https://www.cnblogs.com/zpcoding/p/10645726.html
//#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <utility>
#include <vector>
#include <stack>
#include <cstring>
#include<unordered_map>
using namespace std;
vector<string> split(const string& s, const string& seperator) {
vector<string> result;
unsigned int posBegin = 0;
unsigned int posSeperator = s.find(seperator);
while (posSeperator != s.npos) {
result.push_back(s.substr(posBegin, posSeperator - posBegin));//
posBegin = posSeperator + seperator.size(); // 分隔符的下一个元素
posSeperator = s.find(seperator, posBegin);
}
if (posBegin != s.length()) // 指向最后一个元素,加进来
result.push_back(s.substr(posBegin));
return result;
}
void splitTest(const string& str,string& symbol) {
vector<string> result;
result = split(str, symbol);
for (auto & i : result) {
cout <<atoi(i.c_str())<< endl; // 把数字字符串转换为 数字
}
}
//https://blog.csdn.net/qq_44733706/article/details/100799773
std::string getSths(const string& str ,int(*isSthFunc)(int)){
string outStr;
for(char ch:str){
if(isSthFunc(ch)){
outStr+=ch;
}
}
return outStr;
}
std::string getNums(const string& str){
// http://c.biancheng.net/ref/4.html
// isdigit('1');
return getSths(str,isdigit);
}
/*std::string getNums(const string& str){
string outStr;
for(char ch:str){
if(isdigit(ch)){
outStr+=ch;
}
}
return outStr;
}*/
int isHex(int ch){
return isdigit(ch)||(ch>='a'&&ch<='f')||(ch>='A'&&ch<='F');
}
std::string getHexes(const string& str){
return getSths(str,isHex);
/* string outStr;
for(char ch:str){
if(isHex(ch)){
outStr+=ch;
}
}
return outStr;*/
}
std::string replace(std::string oldStr,const std::string& dontWant,const std::string& toBe){
int len=oldStr.length();
int dontWantLen=dontWant.length();
// https://www.cnblogs.com/wkfvawl/p/9429128.html
for(int i=0;i<len;){
int pos=oldStr.find(dontWant,i);
if(pos==string::npos)break;
oldStr=oldStr.replace(pos,dontWantLen,toBe);
i+=dontWantLen;
}
return oldStr;
}
/*
void stringSamePart(const std::string& a,const std::string& b){
int aLen=a.length();
int bLen=b.length();
}
*/
#include <iostream>
#include <string>
using namespace std;
#include "StringInfo.h"
vector<StringInfo> maxCommonSubstrInfo(const string& s1, const string& s2)
{
string s3 = (s1.length() <= s2.length())?s1:s2;
string s4 = (s1.length() <= s2.length())?s2:s1;
int m = s3.length();
int n = s4.length();
int **dp = new int*[m+1];
for(int i = 0; i < m+1; i++)
{
dp[i] = new int[n+1];
for(int j = 0; j < n+1; j++)
dp[i][j] = 0;
}
int start = 0, maxlen = 0;
for(int i = 1; i < m+1; i++)
{
for(int j = 1; j < n+1; j++)
{
if(s3[i-1] == s4[j-1])
{
dp[i][j] = dp[i-1][j-1]+1;
if(dp[i][j] > maxlen)
{
maxlen = dp[i][j];
start = i - maxlen;
}
}
}
}
for(int i = 0; i < m+1; i++)
{
delete dp[i];
dp[i] = nullptr;
}
delete [] dp;
dp = nullptr;
string substr=s3.substr(start,maxlen);
int pos=s4.find(substr);
vector<StringInfo>v;
v.emplace_back( StringInfo(start,maxlen,s3));
v.emplace_back(StringInfo(pos,maxlen,s4));
return v;
// return s3.substr(start, maxlen);
}
//https://blog.csdn.net/na_beginning/article/details/64921406
string maxCommonSubstr(const string& s1, const string& s2)
{
// std::move函数可以以非常简单的方式将左值引用转换为右值引用。
// https://blog.csdn.net/p942005405/article/details/84644069/
// C++ 标准库使用比如vector::push_back 等这类函数时,会对参数的对象进行复制,连数据也会复制.这就会造成对象内存的额外创建, 本来原意是想把参数push_back进去就行了,通过std::move,可以避免不必要的拷贝操作。
// std::move是将对象的状态或者所有权从一个对象转移到另一个对象,只是转移,没有内存的搬迁或者内存拷贝所以可以提高利用效率,改善性能.。
// 对指针类型的标准库对象并不需要这么做.
auto stringInfos= maxCommonSubstrInfo(s1,s2);
auto stringInfo=stringInfos[0];
return stringInfo.getStr().substr(stringInfo.getStartIndex(),stringInfo.getLen());
// return s3.substr(start, maxlen);
}
std::string formatStr(const std::string& str,int every,char putWhat){
std::string outStr;
int cnt=0;
for(char ch:str){
cnt++;
if(cnt==every){
outStr+=ch;
outStr+=putWhat;
cnt=0;
}else{
outStr+=ch;
}
}
return outStr;
}
void printSomeWhat(int howMany, char what) {
for (int i = 0; i < howMany; i++) {
printf("%c", what);
}
}
void showStrInfo(const StringInfo& stringInfo){
const string& string1=stringInfo.getStr();
cout<<string1<<"\n";
int index=stringInfo.getStartIndex();
//index ==9 ,because the first is 0, so we print 9 ' ',then the next
// is the index==9 thing
printSomeWhat(index,' ');
cout<<"^";
int len=stringInfo.getLen();
// -2 means we cut the head and tail ^
printSomeWhat(len-2,' ');
cout<<"^"<<"\n";
cout<<stringInfo<<"\n";
cout<<"substr: "<<string1.substr(index,len)<<"\n";
}
//对齐
void showTwoStrsAlign(vector<StringInfo> stringInfos){
int index0=stringInfos[0].getStartIndex();
int index1=stringInfos[1].getStartIndex();
if(index0<index1){
int maxIndex=index1;
// 比较长 1
int preBlankNum=maxIndex-index0;
printSomeWhat(preBlankNum,' ');
cout<<stringInfos[0].getStr()<<"\n";
cout<<stringInfos[1].getStr()<<"\n";
printSomeWhat(maxIndex,' ');
cout<<"^";
printSomeWhat(stringInfos[0].getLen()-2,' ');
cout<<"^\n";
}
else{
int maxIndex=index0;
// 比较长 1
int preBlankNum=maxIndex-index1;
cout<<stringInfos[0].getStr()<<"\n";
printSomeWhat(preBlankNum,' ');
cout<<stringInfos[1].getStr()<<"\n";
printSomeWhat(maxIndex,' ');
cout<<"^";
printSomeWhat(stringInfos[0].getLen()-2,' ');
cout<<"^\n";
}
}
void testMaxCommonSubstr()
{
string s1, s2;
while(cin >> s1 >> s2)
{
auto strInfos= maxCommonSubstrInfo(s1, s2);
// showStrInfo(strInfos[0]);
// showStrInfo(strInfos[1]);
showTwoStrsAlign(strInfos);
// cout << maxCommonSubstrInfo(s1, s2) << endl;
}
}
std::vector<std::string> getSthsFromVector(const std::vector<std::string>&v,std::string (getSthFunc)(const string&)){
std::vector<std::string>outVector;
for(const string& val:v){
outVector.emplace_back(getSthFunc(val));
}
return outVector;
}
std::vector<std::string> getNumsFromVector(const std::vector<std::string>&v){
return getSthsFromVector(v,getNums);
/*std::vector<std::string>outVector;
for(const string& val:v){
outVector.emplace_back(getNums(val));
}
return outVector;*/
}
std::vector<std::string> getHexesFromVector(const std::vector<std::string>&v){
return getSthsFromVector(v,getHexes);
/* std::vector<std::string>outVector;
for(const string& val:v){
outVector.emplace_back(getNums(val));
}
return outVector;*/
}
//int _tmain(int argc, _TCHAR* argv[])
//{
// string widths = "5 5 5 5 5 5 10 10 10 10 10 10 10 10 10 10 10 10 5 5 5 5 5 5 5 5";
// string symbol = " ";
//
// splitTest(widths,symbol);
//
//
// system("pause");
// return 0;
//}
//