日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【CodeForces - 761C】Dasha and Password (暴力可过,标解dp,字符串,有坑总结)

發布時間:2023/12/10 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 761C】Dasha and Password (暴力可过,标解dp,字符串,有坑总结) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length?n?which satisfies the following requirements:

  • There is at least one digit in the string,
  • There is at least one lowercase (small) letter of the Latin alphabet in the string,
  • There is at least one of three listed symbols in the string: '#', '*', '&'.

Considering that these are programming classes it is not easy to write the password.

For each character of the password we have a fixed string of length?m, on each of these?n?strings there is a pointer on some character. The?i-th character displayed on the screen is the pointed character in the?i-th string. Initially, all pointers are on characters with indexes?1?in the corresponding strings (all positions are numbered starting from one).

During one operation Dasha can move a pointer in one string one character to the left or to the right. Strings are cyclic, it means that when we move the pointer which is on the character with index?1?to the left, it moves to the character with the index?m, and when we move it to the right from the position?m?it moves to the position?1.

You need to determine the minimum number of operations necessary to make the string displayed on the screen a valid password.

Input

The first line contains two integers?n,?m?(3?≤?n?≤?50,?1?≤?m?≤?50)?— the length of the password and the length of strings which are assigned to password symbols.

Each of the next?n?lines contains the string which is assigned to the?i-th symbol of the password string. Its length is?m, it consists of digits, lowercase English letters, and characters '#', '*' or '&'.

You have such input data that you can always get a valid password.

Output

Print one integer — the minimum number of operations which is necessary to make the string, which is displayed on the screen, a valid password.

Examples

Input

3 4 1**2 a3*0 c4**

Output

1

Input

5 5 #*&#* *a1c& &q2w* #a3c# *&#*&

Output

3

Note

In the first test it is necessary to move the pointer of the third string to one left to get the optimal answer.

In the second test one of possible algorithms will be:

  • to move the pointer of the second symbol once to the right.
  • to move the pointer of the third symbol twice to the right.

?

題目大意:

? ? 有 n 個字符串,每個字符串的長度是m,如果認定一個字符串是一個密碼,則必須滿足:

1:至少有1個數字。2:至少有一個小寫字母。3:至少有一個 #、*或&

現在有n個光標,每個光標指向這 n 個字符串?,F在可以移動光標,最后使得所有光標指向的字符能組成一個密碼。(字符串是循環的)

題目保證一定有解。

解題報告:

? ? 這題可以直接暴力啊??戳讼聰祿秶?#xff0c;o(n^3)不超,想法造一個三次方的循環暴力出來?正好題目三個條件,所以每一層循環就是一個條件不就好了?

?

AC代碼:

#include<bits/stdc++.h> #define ll long long using namespace std; const int INF = 1000000; char s[55][55]; int shu[55],zimu[55],cc[55]; int main() {int n,m;scanf("%d%d",&n,&m);for(int i = 1; i<=n; i++) {scanf("%s",s[i]+1);shu[i] = zimu[i] = cc[i] = INF;} // memset(shu,INF,sizeof shu); // memset(zimu,INF,sizeof zimu); // memset(cc,INF,sizeof cc);for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(s[i][j] >='0' && s[i][j] <= '9') {shu[i] = min(shu[i],min(m-j+1,j-1));}} // printf("%d\n",shu[i]);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(s[i][j] >='a' && s[i][j] <= 'z') {zimu[i] = min(zimu[i],min(m-j+1,j-1));}} // printf("%d\n",zimu[i]);} for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(s[i][j] == '#' || s[i][j] == '*' || s[i][j] == '&' ) {cc[i] = min(cc[i],min(m-j+1,j-1));}} // printf("%d\n",cc[i]);} int ans = INF;for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {for(int k = 1; k<=n; k++) {if(i == j || i == k || j == k) continue; // if(ans == 1) printf("%d %d %d\n",i,j,k);ans = min(ans,shu[i]+zimu[j]+cc[k]);}}}printf("%d\n",ans);return 0 ; } //3 5 //***** //1***a //**a**

總結:

? ?1WA在沒有判斷三者處在同一行的情況。

? ?2WA在INF設置的值過大了。。因為這里是三個值相加啊,所以可以考慮一下1<<29之類的數。

? 題目保證一定有解的類型,考慮一下預處理?

這題標解貌似是記憶化搜索?(鏈接)不過其實復雜度貌似也差不多、、

#include<bits/stdc++.h> using namespace::std;const int N=55; const int INF=0x3f3f3f3f;int n, m, dp[N][2][2][2]; char s[N][N];bool isdigit(char ch) {if(ch>='0' && ch<='9')return true;return false; }bool isletter(char ch) {if(ch>='a' && ch<='z')return true;return false; }bool issymbols(char ch) {if(ch=='*' || ch == '&' || ch == '#')return true;return false; }int solve(int pos, bool digit, bool letter, bool symbols) {if(pos==n){if(digit && letter && symbols)return 0;return INF;}if(dp[pos][digit][letter][symbols] != -1)return dp[pos][digit][letter][symbols];int ans=INF;for(int j=0; j<m; j++){int cost = min(j, m-j);if(isdigit(s[pos][j]))ans = min(ans, cost+ solve(pos+1, 1, letter, symbols));else if(isletter(s[pos][j]))ans = min(ans, cost+ solve(pos+1, digit, 1, symbols));else if(issymbols(s[pos][j]))ans = min(ans, cost+ solve(pos+1, digit, letter, 1));}return dp[pos][digit][letter][symbols]=ans; }int main() {scanf("%d%d",&n,&m);for(int i=0; i<n; i++)scanf("%s",s[i]);memset(dp, -1, sizeof(dp));printf("%d\n",solve(0,0,0,0));return 0; }

?

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的【CodeForces - 761C】Dasha and Password (暴力可过,标解dp,字符串,有坑总结)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。