P1032 字串變換

2022-04-29 22:03:12 字數 3778 閱讀 3981

迭代加深難題

右手進入傳送門

大意是這樣的:給定兩個字串a,b以及至多六個變換規則

(規則指a1->b1,a2->b2,在a中的子串 a1可以變換為b1,a2可以變換為 b2 …)

求最少變換步數,若在10步(包含10步)以內能將a變換為b,則輸出最少的變換步數;否則輸出"no answer!「

看到題目要求的10步以內,就先到了最近學的迭代加深

有思路了很好,但是難的是表示搜尋的狀態,因為要找到對應的子串,再進行變換。

這時便可以用乙個玄學東東string

返回乙個char *char型別的指標

關於char[ ]``char *``string這些東西,盡量要用string全部用string,否則都用char[ ],後期兩個轉換自己認為很麻煩的。

+:連線兩個string

string c=a+b;

c="123"+c+"321";

> < == != : 根據字典序比較

inline bool cmp(string a,string b)

...int main()

sort(a+1,a+1+n,cmp);

某個string a.insert(位置,另乙個string)

string str="to be question";

string str2="the ";

str.insert(6,str2);// to be (the )question

刪除從pos開始的n個字元,比如erase(0,1)就是刪除第乙個字元

str.erase(0,3); //(~~to ~~)be question

某個string a.replace(pos,len,另乙個string b)

替換a中pos開始往後len的這些字元為b

str.replace(0,2,"to");// (to) be question

往往與find()一起使用。

完全匹配string b

a.find(b)從開始找b第一次出現的位置並返回

a.find(b,pos)pos開始找b第一次出現的位置並返回

string str="to be, or not to be - that is the question";

int t=str.find("be");\\ t=3,str[t]='b'(to be 的be)

int t=str.find("be",4);\\ t=17,str[t]='b'(not to be的be)

rfind(b)或rfind(b,pos) 倒著找o(∩_∩)o~

int t=str.rfind("be");\\ t=17,str[t]='b'(not to be的be)

int t=str.rfind("be",16);\\ t=3,str[t]='b'(to be 的be)

沒有出現,返回npos,即-1(列印出來為4294967295)

if (str.find("be")==string::npos)

cout <<"no"<在a中尋找string b中任意乙個字元 『(任意乙個)』

a.find_first_of(b)a.find_first_of(b,pos)

a開始(或從pos開始)向後查詢,只要在a中遇到乙個字元,該字元與c中任意乙個字元相同,就停止查詢,返回該字元在a中的位置;若匹配失敗,返回npos

舉個栗子

//將字串中所有的母音字母換成*

\#include\#includeusing namespace std;

int main()

std::cout << str << '\n';

return 0;

} //執行結果:

//pl**s* r*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks

find_last_of 倒著找

感覺和前面一類的相反的,類似於找了個補集。即在a中搜尋b中沒有的字元並返回位置

用法同上,第乙個是string b,第二個可選 pos,不寫pos預設為0

如果將上乙個樣例中的str.find_first_of改成str.find_first_not_of,則輸出會把非a(あ)i(い)u(う)e(え)o(お)(逃。。。)的換成 *

總結一下「找」的函式的傳參(string b,pos,len)

b是被搜尋的物件。pos(可有可無)指出a內的搜尋起點位置,第三個引數len(可有可無)指出b中搜尋的字元個數(即為b的某個字串)。

sub(start,length)

如果第二個引數不寫,就是從start到字串結尾。

string str="to be, or not to be - that is the question";

str.substr(0,2);// to

str.substr(str.find("question"));// question

(沒有用的東西。。)

總結部分 參考資料

c++中的string常用函式用法總結

c++string中用於查詢的find系列函式**

c++中string、char*、char的轉換

c++中的stl中map用法詳解

#include#include#include#include#include#include#include#define maxn 9999999

using namespace std;

string a,b,change1[10],change2[10];

mapv1;

mapv2;

int t=1,d=1,ans=maxn;

void dfs(string now,int step)

if(v1[now])

v1[now]=true;v2[now]=step;

int f;

string x;

for(int i=1;i<=t;i++)

} return;

}int main()

if(ans==maxn)

printf("%d\n",ans);

return 0;

}

正解是bfs,目前沒有理解。。。。。。

迭代加深是很好用的演算法,本題順便加深了對string和map的理解

沒了

P1032 字串變換

思路 採用bfs 我們遍歷字串a的每個字元,判斷當前字串i位置之後可不可以替換,如果可以替換,我們就把替換後的字串 a 放入佇列。如果出現的我們想要的字串,根據bfs的性質,那麼就直接記錄此時的步數。1 include 2 include 3 include 4 include 5 include ...

P1032 字串變換

已知有兩個字串 a,b 及一組字串變換的規則 至多6個規則 a1 b1 a2 b2 規則的含義為 在 a 中的子串 a1 可以變換為 b1 a2 可以變換為 b2 例如 a abcd b xyz 變換規則為 abc xu ud y y yz 則此時,a 可以經過一系列的變換變為 b,其變換的過程為 ...

P1032 字串變換 字串

已知有兩個字串 a,b 及一組字串變換的規則 至多6個規則 a1 b1 a2 b2 規則的含義為 在 a 中的子串 a1 可以變換為 b1 a2 可以變換為 b2 例如 a abcd b xyz 變換規則為 abc xu ud y y yz 則此時,a 可以經過一系列的變換變為 b,其變換的過程為 ...