作者:手机用户2602922607 | 来源:互联网 | 2023-09-08 13:53
原题链接:CCF-CSP 202006-3 Markdown渲染器 写的下面这个代码就是奔着40分去的,结果呢,细节又没把握好,一直只有20分,找错的时候发现自己犯的两个错误: 1.如果一开头就有空格行,需要忽略,无需计数 2.如果一个段落中,前一行没有满w(假设w为10,前一行刚好9个字符),那么需要换到下一个行加字符,这时候就刚好满足了w,需要先存入,再讨论该行的情况
# include using namespace std; const int N&#61; 510 ; bool check ( string s) { for ( int i&#61; 0 ; i< s. size ( ) ; i&#43;&#43; ) { if ( s[ i] !&#61; &#39; &#39; ) return false ; } return true ; } int main ( ) { std:: ios:: sync_with_stdio ( false ) ; cin. tie ( 0 ) ; cout. tie ( 0 ) ; int w; cin>> w; int res&#61; 0 ; cin. ignore ( ) ; string s, t; vector< string> v; int flag&#61; 0 , len&#61; 0 ; while ( ! cin. eof ( ) ) { getline ( cin, s) ; if ( check ( s) ) { flag&#61; 1 ; if ( len> 0 ) v. push_back ( t) ; len&#61; 0 ; t&#61; "" ; continue ; } if ( flag && v. size ( ) > 0 ) v. push_back ( " " ) ; flag&#61; 0 ; int i&#61; 0 , j&#61; s. size ( ) - 1 ; while ( i<&#61; j && s[ i] &#61;&#61; &#39; &#39; ) i&#43;&#43; ; while ( j>&#61; 0 && s[ j] &#61;&#61; &#39; &#39; ) j-- ; s&#61; s. substr ( i, j- i&#43; 1 ) ; i&#61; 0 ; if ( len> 0 ) { len&#43;&#43; ; t&#43;&#61; " " ; if ( len&#61;&#61; w) { len&#61; 0 ; res&#43;&#43; ; v. push_back ( t) ; t&#61; "" ; } } while ( len< w ) { for ( ; i< s. size ( ) && len< w; i&#43;&#43; ) { len&#43;&#43; ; t&#43;&#61; s[ i] ; } if ( len&#61;&#61; w) { len&#61; 0 ; res&#43;&#43; ; v. push_back ( t) ; t&#61; "" ; while ( i< s. size ( ) && s[ i] &#61;&#61; &#39; &#39; ) i&#43;&#43; ; } if ( i&#61;&#61; s. size ( ) ) break ; } }
60分代码&#xff1a;后面的实在是写不出来了&#xff0c;罢了罢了
# include using namespace std; const int N&#61; 510 ; vector< string> v; int w; int res&#61; 0 , len&#61; 0 , f&#61; - 1 , pre&#61; - 1 ; bool check ( string s) { for ( int i&#61; 0 ; i< s. size ( ) ; i&#43;&#43; ) { if ( s[ i] !&#61; &#39; &#39; ) return false ; } return true ; } string drop ( string s) { int i&#61; 0 , j&#61; s. size ( ) - 1 ; while ( i<&#61; j && s[ i] &#61;&#61; &#39; &#39; ) i&#43;&#43; ; while ( j>&#61; 0 && s[ j] &#61;&#61; &#39; &#39; ) j-- ; s&#61; s. substr ( i, j- i&#43; 1 ) ; return s; } int main ( ) { std:: ios:: sync_with_stdio ( false ) ; cin. tie ( 0 ) ; cout. tie ( 0 ) ; cin>> w; cin. ignore ( ) ; string s, t; while ( ! cin. eof ( ) ) { getline ( cin, s) ; if ( check ( s) ) { if ( ! check ( t) ) v. push_back ( t) ; t&#61; "" ; len&#61; 0 ; f&#61; pre&#61; 0 ; } else if ( ( s. size ( ) >&#61; 2 && s[ 0 ] &#61;&#61; &#39;*&#39; && s[ 1 ] &#61;&#61; &#39; &#39; ) || ( pre&#61;&#61; 2 && s. size ( ) >&#61; 2 && s[ 0 ] &#61;&#61; &#39; &#39; && s[ 1 ] &#61;&#61; &#39; &#39; ) ) { f&#61; 2 ; if ( pre&#61;&#61; 0 && v. size ( ) > 0 ) v. push_back ( " " ) ; if ( pre&#61;&#61; 1 ) { if ( ! check ( t) ) v. push_back ( t) ; v. push_back ( " " ) ; t&#61; "" ; len&#61; 0 ; } if ( s. size ( ) >&#61; 2 && s[ 0 ] &#61;&#61; &#39;*&#39; && s[ 1 ] &#61;&#61; &#39; &#39; ) { if ( pre&#61;&#61; 2 && ! check ( t) ) v. push_back ( t) ; t&#61; " . " ; len&#61; 3 ; s&#61; s. substr ( 2 ) ; s&#61; drop ( s) ; int i&#61; 0 ; while ( len< w) { for ( ; i< s. size ( ) && len< w ; i&#43;&#43; ) { t&#43;&#61; s[ i] ; len&#43;&#43; ; } if ( len&#61;&#61; w) { if ( ! check ( t) ) v. push_back ( t) ; len&#61; 3 ; t&#61; " " ; while ( i< s. size ( ) && s[ i] &#61;&#61; &#39; &#39; ) i&#43;&#43; ; } if ( i&#61;&#61; s. size ( ) ) break ; } } else if ( pre&#61;&#61; 2 && s. size ( ) >&#61; 2 && s[ 0 ] &#61;&#61; &#39; &#39; && s[ 1 ] &#61;&#61; &#39; &#39; ) { if ( len> 0 ) { if ( t!&#61; " . " ) { t&#43;&#61; " " ; len&#43;&#43; ; if ( len&#61;&#61; w) { if ( ! check ( t) ) v. push_back ( t) ; t&#61; " " ; len&#61; 3 ; } } } s&#61; s. substr ( 2 ) ; s&#61; drop ( s) ; int i&#61; 0 ; while ( len< w) { for ( ; i< s. size ( ) && len< w ; i&#43;&#43; ) { t&#43;&#61; s[ i] ; len&#43;&#43; ; } if ( len&#61;&#61; w) { if ( ! check ( t) ) v. push_back ( t) ; len&#61; 3 ; t&#61; " " ; while ( i< s. size ( ) && s[ i] &#61;&#61; &#39; &#39; ) i&#43;&#43; ; } if ( i&#61;&#61; s. size ( ) ) break ; } } pre&#61; 2 ; } else { f&#61; 1 ; if ( pre&#61;&#61; 0 && v. size ( ) > 0 ) { v. push_back ( " " ) ; t&#61; "" ; len&#61; 0 ; } if ( pre&#61;&#61; 2 ) { if ( ! check ( t) ) v. push_back ( t) ; v. push_back ( " " ) ; t&#61; "" ; len&#61; 0 ; } if ( len> 0 ) { t&#43;&#61; " " ; len&#43;&#43; ; if ( len&#61;&#61; w) { if ( ! check ( t) ) v. push_back ( t) ; t&#61; "" ; len&#61; 0 ; } } s&#61; drop ( s) ; int i&#61; 0 ; while ( len< w) { for ( ; i< s. size ( ) && len< w ; i&#43;&#43; ) { t&#43;&#61; s[ i] ; len&#43;&#43; ; } if ( len&#61;&#61; w) { if ( ! check ( t) ) v. push_back ( t) ; len&#61; 0 ; t&#61; "" ; while ( i< s. size ( ) && s[ i] &#61;&#61; &#39; &#39; ) i&#43;&#43; ; } if ( i&#61;&#61; s. size ( ) ) break ; } pre&#61; 1 ; } }