①auto use
#include<set>
#include<iostream>
#include<cstdio>
#include<algorithm>
#define for0(x) for (int i = 0; i < x ; i++)
#define for1(x) for (int i = 1; i <= x ; i++)
using namespace std;
set<int>s;
int main()
{
///auto 用法1:自动判断迭代器类型
int x;
for1(10) cin>>x,s.insert(x);
for (auto i = s.begin();i != s.end(); i++ )
cout << *i << " ";
cout << endl;
///auto 用法2:对于容器可以直接方便的遍历
//string
string s;
cin >> s;
for (auto &c:s){///加上引用可以对容器内值直接修改
c++;
}
cout << s << endl;
//vector
vector<int> v;
for1(10) cin>>x,v.push_back(x);
for (auto i:v)
cout << i << " ";
cout << endl;
return 0;
}
②HDU 1867 harvest
(1) how easy it is output string A, B to remove the top few string concatenation
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
char s[10000],ss[10000];
int main()
{
cin >> s >> ss;
int x,y;
cout << "第一个字符串去掉前x位,第二个去掉前y位,输入x,y" << endl;
cin >> x >> y;
printf("%s%s\n",s+x,ss+y);
return 0;
}
(2) re-comb the next array structure kmp process
i represents the string ending at i
j represents the current longest string match with the suffix
By character comparison position i and j, determines whether the longest suffix +1
If not, the use of already part of the current match, find the longest common prefix and suffix of this section, j went to that location, this is the largest prefix and suffix after adaptation, again
Comparing the current position, if successful, then the suffix +1
(3) up to about the same value for the prefix and suffix strings solving A problem string B
Method a: spliced together, while the last until the desirable
Method Two:
1.B string request next.
2.A string with string B to seek next, so that the last match after finished i, j to i point is the end of the string (A) and the longest prefix and suffix B
(4) on the difference between (3) and two main methods string matching substrings
Respectively referred to the case 1, case 2
Longest prefix suddenly found Scenario 2 is in position i find a large string end of the string and the suffix string of small
While when j = lenm, the main character string is definitely not equal to '\ 0' so in this case j is the backoff
If the cut is similar to the cloth, then after the completion of each match, let j = 0, because before the cut, now equivalent to start again
③exkmp board + their own way when the three figures
const int maxn=100010; //字符串长度最大值
int next[maxn],ex[maxn]; //ex数组即为extend数组
//预处理计算next数组
void GETNEXT(char *str)///求子串KMP
{
int i=0,j,po,len=strlen(str);
next[0]=len;///初始化next[0],为什么初始化为len
while(str[i]==str[i+1]&&i+1<len) i++;///计算next[1]
next[1]=i;
///为什么暴力匹配next[1],因为next[0] = len,前缀和后缀重合,没有意义
///简单来说,就是这么搞会错
po=1;//初始化po的位置
for(i=2;i<len;i++)
{
///疑问:j<0的情况不包含在第一种情况中吗
///也就是保证相等的部分在当前位置的前面
///哪些情况会进入第一种?P延伸的距离很短且没有到,那么此时直接等于这个肯定不对,证明这种情况没有进情况1
///当前问题:对第一种第二种的具体界定?
/// 第一种情况:已知相同(并非匹配)部分 前移(与PO位置有关)所在位置next(也就是定义的len)小于相同长度,那么就等于len
/// j<0表示当前没有相同段,不管你要和那个next比,最小就是0,
///也就是 (>=0的数) >= 0,进入else
if(next[i-po]+i<next[po]+po)//第一种情况,可以直接得到next[i]的值
next[i]=next[i-po];
else//第二种情况,要继续匹配才能得到next[i]的值
{ /// P = PO + next[PO] -1,表示保证相同的一部分的最远距离
j=next[po]+po-i;/// 等价于 P -i + 1,也就是相同部分的长度
if(j<0)j=0;///如果i>po+next[po],则要从头开始匹配,应该挺常见的情况
while(i+j<len&&str[j]==str[j+i])///计算next[i],从第一个条件退出循环表示后缀都匹配完了,把这个条件放前面否则可能下标越界
j++;
next[i]=j;
po=i;//更新po的位置
}
}
}
//计算extend数组
void EXKMP(char *s1,char *s2)///这个部分根据kmp匹配的原理,就是在以小串做前缀对照,来求大串的对应最长后缀
{ ///唯一不同,这里暴力求ex[0],没有之前那个前后缀重合的烦恼
int i=0,j,po,len=strlen(s1),l2=strlen(s2);
GETNEXT(s2);//计算子串的next数组
while(s1[i]==s2[i]&&i<l2&&i<len)//计算ex[0]
i++;
ex[0]=i;
po=0;//初始化po的位置
for(i=1;i<len;i++)
{
if(next[i-po]+i<ex[po]+po)//第一种情况,直接可以得到ex[i]的值
ex[i]=next[i-po];
else//第二种情况,要继续匹配才能得到ex[i]的值
{
j=ex[po]+po-i;
if(j<0)j=0;//如果i>ex[po]+po则要从头开始匹配
while(i+j<len&&j<l2&&s1[j+i]==s2[j])//计算ex[i]
j++;
ex[i]=j;
po=i;//更新po的位置
}
}
}
Demand situation next array, enter if the example, and why not directly get the results continue to match
Ex seek array
j <0