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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

极度 YM....自己用trie写了个伪 map....( HDU 1075 )

發布時間:2023/12/18 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 极度 YM....自己用trie写了个伪 map....( HDU 1075 ) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

MiYu原創, 轉帖請注明 : 轉載自?______________白白の屋????

?

這幾天一直很YM..... 糾結.....??做個水題 1075......WA N次.....YM.
(??很簡單的一題,??檢索輸入的串是否存在, 存在就替換輸出, 不存在直接輸出就可以了 )
最后無奈, 純C++ STL..過了, 時間竟然用了 1600+MS 還是 C++交的,??G++要3400+MS,??不
明白為什么時間差那么多,??用trie 做了次, 250MS...? ?改了好幾次效率都上不來???....??YM 中 把trie 寫成了 偽 map ,只有一點簡單的功能,
其他的不想寫了.??寫得好復雜....................?
偽 map 代碼如下 :??好長.....................
/*MiYu原創, 轉帖請注明 : 轉載自 ______________白白の屋http://www.cnblog.com/MiYuAuthor By : MiYuTest ? ? ?: 4Program ? : 1075*/
#include <iostream>#include <string>using namespace std;typedef struct dict DIC;DIC *root = NULL;string ext = "";struct dict {dict (){ str = "";memset ( child , 0 , sizeof ( child ) ); }~dict () { }void del ( DIC * node );string & insert ( char *ins,char *key = NULL );string & find ( char *ins );string & operator [] ( char *a );string & operator = ( char *a );DIC *child[26];string str;?};void dict::del ( DIC * node ){if ( node == NULL ) return;for ( int i = 0; i != 26; ++ i ){if ( node->child[i] != NULL ) ?del ( node->child[i] );}node->str = "";free ( node ); ?}string & dict::operator [] ( char *a ){string &str = find ( a );if ( str == "" )return ?insert ( a ); ?return str;}string & dict::operator = ( char *a ){return this->str = a;?}string & dict::insert ( char *ins,char *key ){DIC *cur = this,*now;int len = strlen ( ins );for ( int i = 0; i != len; ++ i ){if ( cur->child[ ins[i] - 'a' ] != NULL )?{cur = cur->child[ ins[i] - 'a' ];}else{now = new DIC;cur->child[ ins[i] - 'a' ] = now;cur = now;}}?return cur->str;}string & dict::find ( char *ins ){DIC *cur = this;int len = strlen ( ins );for ( int i = 0; i != len; ++ i ){if ( cur->child[ ins[i] - 'a' ] != NULL )cur = cur->child[ ins[i] - 'a' ]; ?elsereturn ext;?}?return cur->str;}char words[3010],temp[12],t[12];int main (){DIC dict;root = &dict;scanf ( "%s", t );while ( scanf ( "%s", t ), strcmp ( t, "END" ) != 0 ){scanf ( "%s", temp );dict[temp] = t;}scanf ( "%s", t );getchar();while ( gets ( words ) && strcmp ( words, "END" ) != 0 ){memset ( temp, 0, sizeof ( temp ) );int len = strlen ( words );for ( int i = 0,j = 0; i != len; ++ i ){if ( isalpha ( words[i] ) ){temp[j++] = words[i];}?else?{temp[j] = '\0';string str = dict[ temp ];if ( str == "" )printf ( "%s",temp );?elseprintf ( "%s",str.c_str() );putchar ( words[i] );j = 0;?}}putchar ( 10 );}return 0;}

?

?STL 代碼如下 : ?好短....

?/*

MiYu原創, 轉帖請注明 : 轉載自 ______________白白の屋

?? ? ? ? ?http://www.cnblog.com/MiYu

Author By : MiYu

Test ? ? ?: 1

Program ? : 1075

*/


#include <iostream>

#include <string>

#include <map>

using namespace std;

string words,temp;

map < string , string > mp;

int main ()

{

?? ?cin >> words;

?? ?while ( cin >> words, words != "END" )

?? ?{

?? ? ? ? ? cin >> temp;

?? ? ? ? ? mp[ temp ] = words;

?? ?}

?? ?cin >> words;

?? ?getchar();

?? ?while ( getline ( cin, words ) && words != "END" )

?? ?{

?? ? ? ? ? string out = "";

?? ? ? ? ? int len = words.size();

?? ? ? ? ? for ( int i = 0; i != len; ++ i )

?? ? ? ? ? {

?? ? ? ? ? ? ? ?if ( isalpha ( words[i] ) )

?? ? ? ? ? ? ? ?{

?? ? ? ? ? ? ? ? ? ? out += words[i];

?? ? ? ? ? ? ? ?}?

?? ? ? ? ? ? ? ?else?

?? ? ? ? ? ? ? ?{

?? ? ? ? ? ? ? ? ? ? if ( mp[out] == "" )

?? ? ? ? ? ? ? ? ? ? ? ? ?cout << out;?

?? ? ? ? ? ? ? ? ? ? else

?? ? ? ? ? ? ? ? ? ? ? ? ?cout << mp[out];

?? ? ? ? ? ? ? ? ? ? cout << words[i];

?? ? ? ? ? ? ? ? ? ? out = "";?

?? ? ? ? ? ? ? ?}

?? ? ? ? ? }

?? ? ? ? ? cout << endl;

?? ?}

?? ?return 0;

}


?

?

?

?

轉載于:https://www.cnblogs.com/MiYu/archive/2010/08/25/1807990.html

總結

以上是生活随笔為你收集整理的极度 YM....自己用trie写了个伪 map....( HDU 1075 )的全部內容,希望文章能夠幫你解決所遇到的問題。

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