牛客练习赛23 托米的咒语
生活随笔
收集整理的這篇文章主要介紹了
牛客练习赛23 托米的咒语
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
托米沒有完成上一個任務,準備施展黑魔法推倒 1317
黑魔法咒語被描述為一個 長為 n 的,僅包含小寫英文字母 'a'...'i' 的字符串,在托米所在的星球,魔法造成的每次有效傷害都是來自他的一個子序列,對于每一個 'a'... 'i' 的排列(共 9! 種),若作為咒語的子序列出現, 就會造成 1 的傷害 而咒語的總傷害為所有 'a'... 'i' 的排列造成的傷害值之和,托米能打出多少點的傷害,是否能擊敗 1317 呢?
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 #include <string> 6 #include <string> 7 #include <map> 8 #include <cmath> 9 #include <set> 10 #include <algorithm> 11 using namespace std; 12 typedef long long ll; 13 const int N=1e5+7; 14 char p[10]="abcdefghi"; 15 string s; 16 int dp[3100][10],num[10]; 17 ll ans; 18 int flag; 19 int main() 20 { 21 cin>>s; 22 s=' '+s; 23 for(int i=s.size()-1;i>=0;i--){ 24 for(int j=0;j<9;j++){ 25 dp[i][j]=num[j];//例如dp[1][0]:輸入字符串里第一個 26 //字符串右邊‘a’距離1最近的位置。 27 /* 28 為什么要最近的 29 舉個例子: 30 abcbde 31 a 若匹配第二個b就無法查找到abcde,但是abcde是存在的. 32 也就是說離彼此最近的都無法找到,就一定不存在了. 33 */ 34 } 35 if(i!=0) num[s[i]-'a']=i;//不斷更新. 36 } 37 /* 38 ''aabcdefghi 39 0 0 0 0 0 0 0 0 0 0 40 0 0 0 0 0 0 0 0 10 0 41 0 0 0 0 0 0 0 9 10 0 42 0 0 0 0 0 0 8 9 10 0 43 0 0 0 0 0 7 8 9 10 0 44 0 0 0 0 6 7 8 9 10 0 45 0 0 0 5 6 7 8 9 10 0 46 0 0 4 5 6 7 8 9 10 0 47 0 3 4 5 6 7 8 9 10 0 48 2 3 4 5 6 7 8 9 10 0 49 1 3 4 5 6 7 8 9 10 0 50 */ 51 ans=0; 52 do{ 53 flag=0; 54 for(int i=0;i<9;i++){ 55 flag=dp[flag][p[i]-'a']; 56 if(!flag) {ans--;break;} 57 } 58 ans++; 59 }while(next_permutation(p,p+9));//檢測每個全排列字符串是否存在. 60 printf("%lld\n",ans); 61 return 0; 62 }
黑魔法咒語被描述為一個 長為 n 的,僅包含小寫英文字母 'a'...'i' 的字符串,在托米所在的星球,魔法造成的每次有效傷害都是來自他的一個子序列,對于每一個 'a'... 'i' 的排列(共 9! 種),若作為咒語的子序列出現, 就會造成 1 的傷害 而咒語的總傷害為所有 'a'... 'i' 的排列造成的傷害值之和,托米能打出多少點的傷害,是否能擊敗 1317 呢?
輸入描述:
一行輸入一個字符串 s輸出描述:
一行輸出一個數,表示傷害值 示例1輸入
復制 aabcdefghi輸出
復制 1備注:
|s| ≤ 30001 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 #include <string> 6 #include <string> 7 #include <map> 8 #include <cmath> 9 #include <set> 10 #include <algorithm> 11 using namespace std; 12 typedef long long ll; 13 const int N=1e5+7; 14 char p[10]="abcdefghi"; 15 string s; 16 int dp[3100][10],num[10]; 17 ll ans; 18 int flag; 19 int main() 20 { 21 cin>>s; 22 s=' '+s; 23 for(int i=s.size()-1;i>=0;i--){ 24 for(int j=0;j<9;j++){ 25 dp[i][j]=num[j];//例如dp[1][0]:輸入字符串里第一個 26 //字符串右邊‘a’距離1最近的位置。 27 /* 28 為什么要最近的 29 舉個例子: 30 abcbde 31 a 若匹配第二個b就無法查找到abcde,但是abcde是存在的. 32 也就是說離彼此最近的都無法找到,就一定不存在了. 33 */ 34 } 35 if(i!=0) num[s[i]-'a']=i;//不斷更新. 36 } 37 /* 38 ''aabcdefghi 39 0 0 0 0 0 0 0 0 0 0 40 0 0 0 0 0 0 0 0 10 0 41 0 0 0 0 0 0 0 9 10 0 42 0 0 0 0 0 0 8 9 10 0 43 0 0 0 0 0 7 8 9 10 0 44 0 0 0 0 6 7 8 9 10 0 45 0 0 0 5 6 7 8 9 10 0 46 0 0 4 5 6 7 8 9 10 0 47 0 3 4 5 6 7 8 9 10 0 48 2 3 4 5 6 7 8 9 10 0 49 1 3 4 5 6 7 8 9 10 0 50 */ 51 ans=0; 52 do{ 53 flag=0; 54 for(int i=0;i<9;i++){ 55 flag=dp[flag][p[i]-'a']; 56 if(!flag) {ans--;break;} 57 } 58 ans++; 59 }while(next_permutation(p,p+9));//檢測每個全排列字符串是否存在. 60 printf("%lld\n",ans); 61 return 0; 62 }
?
轉載于:https://www.cnblogs.com/tingtin/p/9384026.html
總結
以上是生活随笔為你收集整理的牛客练习赛23 托米的咒语的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BUG: scheduling whil
- 下一篇: 三明一中2021查询高考成绩,2021年