ä½ã®ãã£ã¡ãï¼ã¨ããåããã«ããã¿ã¤ãã«ã§ãããAIZU ONLINE JUDGEã®åé¡ã§åºã¦ãã¾ãã¦ãå ¥åèªä½ã¯1è¡ã®æååã§ã¯ããããã©ããªã³ã°ç¶ã«ãªã£ã¦ããã¨èãã¦ãã©ãããæ°ãå§ãã¦ãè¯ãã¨ã
ã§ããã1ã¤ã®å ¥åå¤ã®æååãä½ãããã©ãããå¤å®ããã¨ã
â§Â ãå ¥åå¤ï¼ãããªã³ã°ç¶ã«ãã¦ãä»»æã®ä½ç½®ããåãåºããæååã§ããå ¥åå¤ï¼ãã表ç¾ã§ãããã©ããã§ãããããã®å ´åã¯ãYesã
ã§ããå ¥åå¤ï¼ããå¤ãããããå ¥åå¤ï¼ãããadvanced ã¨ãã«ãªãã¨ãNoã
ã¨ãããããªå¤å®ãããã°ã©ãã³ã°ããã¨ã
Â
ããã¦ãå ¨ç¶é¢ä¿ãªããã©ã
simplearchitect.hatenablog.com
simplearchitect.hatenablog.com
â§Â ãã¯ããåè¡å½ã¯ã¹ã´ã¤ãããã§ãã...ãã£ã¹ã«ãã·ã§ã³ã楽ããã£ã¦æè¦ã¨ãæã¦ãããã«ãªãããã§ããã
ã¨ãããã¨ã§ãä»åã¯ãJavaã§ãã
ããã§ã¯ãã¬ããï½ãã©ã¤ã
Â
èãæ¹
ã©ãå®è£ ããã°è¯ãã®ãããï¼
æååsã¯ãªã³ã°ç¶ã®ãã®ã¨ãã¦æ±ã£ã¦ã¯ãã¾ããã
ããã°ã©ã ä¸ã¯ãã ã®æåå(é
å)ã§ãã
ç®çã®æåé ãå¾ãã«ãã£ãå ´åã
ãã®ã¤ã³ããã¯ã¹ãåºç¹ã«+1,+2,+3...ã¨èª¿ã¹ã¦ããã¨
é
åã®é·ããè¶
ãã¦ãã¾ãå ´åãããã¾ãã
ããã«ç¡çããã¢ã¯ã»ã¹ãããã¨ããã¨IndexOutOfBoundsã¨ãªãã ããªã®ã§
ã¤ã³ããã¯ã¹ãé
åã®é·ãã¨åãã«ãªã£ãå ´åã¯ã
ã¤ã³ããã¯ã¹ãé
åã®é·ãåå¼ãã¦ãé
åã®é ã«æ»ãå¿
è¦ãããã¾ãã
ã¤ã³ããã¯ã¹ãé
åã®é·ãã§å²ã£ãä½ããæ±ãããã¨ããã®ã¯ã
ä¸ã®ãããªå¦çã1è¡ã§æ¸ããã®ã§ããã®æã®å¦çã§ã¯ããã¤ãããã¦ãã¾ãã
â§Â teratail ã§ä¸ãã£ã¦ã¾ãããã
ãã¸ã£ãã...ãã使ãããã£ã¦...åãã¦ç¥ãã¾ãããï¼æ¶ï¼
Â
å®éã«å®è£ ãã¦ã¿ã
ã©ããªæãã®ã½ã¼ã¹ã«ãªããã¨ããã¨ãÂ
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class TestRing { public static void main(String[] args) { // TODO èªåçæãããã¡ã½ããã»ã¹ã¿ã BufferedReader kb = new BufferedReader( new InputStreamReader( System.in ) ); try { String ringStr = kb.readLine(); // ãªã³ã°ç¶ã®æåå String targetStr = kb.readLine(); // 対象ã®æåå int ringStrLen = ringStr.length(); // ãªã³ã°ç¶ã®æååã®é·ã int targetStrLen = targetStr.length(); // 対象ã®æååã®é·ã int j = 0; for( int i = 0; i < ringStrLen; i++ ) { if( targetStr.charAt( 0 ) == ringStr.charAt( i ) ) { // ãªã³ã°ç¶ã®æååã«ããã対象ã®æååã®ã¤ã³ããã¯ã¹ç¨ int ij = i; for( j = 0; j < targetStrLen; j++ ) { // ãªã³ã°ç¶ã®æååã®ã¤ã³ããã¯ã¹ãè¶ ããªãããã«å°ä½æ¼ç® ij = ( i+j ) % ringStrLen; // æåãä¸è´ããªãå ´åããªã³ã°ç¶ã®æååã®æ¬¡ã®æåã«é²ããããå¦çãæãã if( targetStr.charAt( j ) != ringStr.charAt( ij ) ) { break; } } } // 対象ã®æååãè¦ã¤ãã£ãå ´åãå¦çãæãã if( targetStrLen == j ) { break; } } // 対象ã®æååãè¦ã¤ãã£ãå ´åãj = 0 ã«ãªã»ãããããªãã®ã§ã if( targetStrLen == j ) { System.out.println( "Yes" ); } else { System.out.println( "No" ); } } catch( IOException e ) { System.err.println( e ); } } }
ãã§ãå®è¡ã
ãªã³ã°ç¶ã®æååããä½æã§ããå ´å
ãªã³ã°ç¶ã®æååããä½æã§ããªãå ´å
Â
teratail.comâ§Â teratail ã®è§£èª¬ãåèã«ããã¨ã
i = 16 ã§ä¸è´ããæåã§æ¤è¨¼ãã¹ã¿ã¼ãããå ´åããij = ( i+j ) % ringStrLenãã£ã¦é¨åã¯ãringStrLen = 18 ã«ãªãï¼ãvanceknowledgetoadããªã®ã§ãæååã®é·ãã¯18ï¼ã®ã§ãããããã¦ã
i | j | ij = ( i+j ) % ringStrLen |
---|---|---|
16 | 0 | 16 |
16 | 1 | 17 |
16 | 2 | 0 |
16 | 3 | 1 |
16 | 4 | 2 |
16 | 5 | 3 |
16 | 6 | 4 |
ã£ã¦ãã¨ã«ãªããããã
ãï½ããä¸æããã¨ããªã³ã°ç¶ã®æååã®æåã®ã»ãã®æåãåç §ããããã«ãªã£ã¦ã¾ããããªãããããã¾ãç解ã¯ã§ããªããã ãã©ããããããã¸ãã¯ããããã ã¨ãããã¨ã§ã
ä»åã¯ãã®ã¸ãã§ã
Â
Â